【发布时间】:2017-09-19 11:07:10
【问题描述】:
在下面的bash 中,我尝试使用awk 来验证headers 的顺序在tab-delimited 文件之间完全相同(key 具有字段顺序和text files ,通常是一个目录中的 3 个)。
如果顺序正确或在文件之间找到匹配项,则print FILENAME 具有预期的字段顺序,但如果文件之间的顺序不匹配,则print FILENAME 导致“$i 的顺序为不正确”,其中$i 是使用key 作为顺序的字段乱序。谢谢你:)
键
Index Chr Start End Ref Alt Inheritance Score
file1.txt
Index Chr Start End Ref Alt Inheritance Score
1 1 10 100 A - . 2
file2.txt
Index Chr Start End Ref Alt Inheritance
1 1 10 100 A - . 2
2 1 20 100 A - . 5
file3.txt
Index Chr Start End Ref Alt Inheritance
1 1 10 100 A - . 2
2 1 20 100 A - . 5
3 1 75 100 A - . 2
4 1 25 100 A - . 5
awk
for f in /home/cmccabe/Desktop/validate/*.txt ; do
bname=`basename $f`
awk '
FNR==NR {
order=(awk '!seen[$0]++ {lines[i++]=$0}
END {for (i in lines) if (seen[lines[i]]==1) print lines[i]})'
k=(awk '!seen[$0]++ {lines[i++]=$0}
END {for (i in lines) if (seen[lines[i]]==1) print lines[i]})'
if($order==$k) print FILENAME " has expected order of fields"
else
print FILENAME " order of $i is not correct"
}' key $f
done
期望的输出
/home/cmccabe/Desktop/validate/file1.txt has expected order of fields
/home/cmccabe/Desktop/validate/file2.txt order of Score is not correct
/home/cmccabe/Desktop/validate/file3.txt order of Score is not correct
【问题讨论】:
-
所有文件都只有一行吗?
-
不抱歉,每个文本文件中有多行,长度可能会有所不同....我会更新帖子。但是每个文件的文本文件中的标题行始终为 1。
key只有 1 行。谢谢你:)。 -
您将 awk 与 shell 混淆了。 Awk 不是外壳。您可以从 Arnold Robbins 的《Effective Awk Programming, 4th Edition》一书中学习 awk。