使用 awk 命令操作列
using awk command to manipulate columns
我在一个目录中有大约 175 个制表符分隔的 txt
文件。我对第一列感兴趣,我想从每个文件的第一列中删除所有重复的项目,然后将它们打印为新的 txt
文件中的列。
#this removes all duplicates in column 1 of myFile.txt
awk '!x[]++' 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 }' $file) > $OUT.tmp
mv $OUT.tmp $OUT
done
我的问题是,我如何组合这两个命令,以便将每个文件的第 1 列(没有重复项)作为列打印到新文件中?
在新文件中打印(每个原始 txt 一个),只有第一列按文件第一次出现(原始文件名 + .filtered.txt
)
awk '!( in F){F[]++; print > FILENAME ".filtered.txt" }' *.txt
如果需要 uniq PER 文件(感谢@karakfa 的评论)
awk '!( ","FILENAME in F){F[","FILENAME]++; print > FILENAME ".filtered.txt" }' *.txt
我在一个目录中有大约 175 个制表符分隔的 txt
文件。我对第一列感兴趣,我想从每个文件的第一列中删除所有重复的项目,然后将它们打印为新的 txt
文件中的列。
#this removes all duplicates in column 1 of myFile.txt
awk '!x[]++' 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 }' $file) > $OUT.tmp
mv $OUT.tmp $OUT
done
我的问题是,我如何组合这两个命令,以便将每个文件的第 1 列(没有重复项)作为列打印到新文件中?
在新文件中打印(每个原始 txt 一个),只有第一列按文件第一次出现(原始文件名 + .filtered.txt
)
awk '!( in F){F[]++; print > FILENAME ".filtered.txt" }' *.txt
如果需要 uniq PER 文件(感谢@karakfa 的评论)
awk '!( ","FILENAME in F){F[","FILENAME]++; print > FILENAME ".filtered.txt" }' *.txt