使用awk命令来操作列

时间:2015-12-14 08:08:46

标签: awk multiple-columns

我在目录中有大约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列(没有重复项)打印成新文件作为列?

1 个答案:

答案 0 :(得分:0)

在新文件中打印(每个原始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