【问题标题】:using awk command to manipulate columns使用 awk 命令来操作列
【发布时间】:2015-12-14 08:08:45
【问题描述】:

我在一个目录中有大约 175 个制表符分隔的 txt 文件。我对第一列感兴趣,我想从每个文件的第一列中删除所有重复的项目,然后将它们打印为新的txt 文件中的列。

#this removes all duplicates in column 1 of myFile.txt
awk '!x[$1]++' myFile.txt 

#this copies all coulmn 1 from every file and paste them as columns in a new file 

#!/bin/bash
OUT=AllColumns.tsv
touch $OUT

for file in *.txt
do
   paste $OUT <(awk -F\\t '{print $1}' $file) > $OUT.tmp
   mv $OUT.tmp $OUT
done

我的问题,我如何结合这两个命令,以便将每个文件中的第 1 列(没有重复项)作为列打印到新文件中?

【问题讨论】:

  • 你是这个意思吗? awk '{a[$1]}END{for(x in a)print x}' *.txt
  • 显示几个输入文件的几行以及输出应该是什么。我不确定您所说的“.. 作为列进入新文件”是什么意思。您的意思是应该有一个包含多个列的输出文件,每一列对应于其中一个输入文件的第 1 列?

标签: awk multiple-columns


【解决方案1】:

在新文件中打印(每个原始 txt 1 个),仅第一次出现第一列(原始文件名 + .filtered.txt

awk '!( $1 in F){F[$1]++; print $1 > FILENAME ".filtered.txt" }' *.txt

如果需要 uniq PER 文件(感谢@karakfa 的评论)

awk '!( $1","FILENAME in F){F[$1","FILENAME]++; print $1 > FILENAME ".filtered.txt" }' *.txt

【讨论】:

  • 这将打印所有文件中的唯一项目,而不是每个文件唯一。可能是也可能不是 OP 所要求的。
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-14
  • 2013-07-29
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
相关资源
最近更新 更多