【问题标题】:awk to check order of header line in text filesawk 检查文本文件中标题行的顺序
【发布时间】: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。

标签: bash awk


【解决方案1】:
$ cat tst.awk
NR==FNR { split($0,keys); next }
FNR==1 {
    allmatched = 1
    for (i=1; i in keys; i++) {
        if ($i != keys[i] ) {
            printf "%s order of %s is not correct\n", FILENAME, keys[i]
            allmatched = 0
        }
    }
    if ( allmatched ) {
        printf "%s has expected order of fields\n", FILENAME
    }
    nextfile
}

$ awk -f tst.awk key file1 file2 file3
file1 has expected order of fields
file2 order of Score is not correct
file3 order of Score is not correct

以上使用 GNU awk 表示 nextfile 以提高效率。使用其他 awk 只需删除该语句并接受将读取每个文件的整个内容。

您没有在示例中包含标题出现在文件中但键中不存在的情况,因此我认为这不会发生,因此您不需要脚本来处理它。

【讨论】:

  • 我正在学习 awk 并阅读有效的 Awk 编程,这很有帮助,但还有很多 :),更不用说他关于 shell 的好书了。谢谢你:)。
【解决方案2】:

鉴于这些输入,您可以执行以下操作:

awk 'FNR==NR{hn=split($0,header); next} 
     FNR==1 {n=split($0,fh)
            for(i=1;i<=hn; i++)
                if (fh[i]!=header[i]) {
                    printf "%s: order of %s is not correct\n" ,FILENAME, header[i]
                    next}
            if (hn==n)
                print FILENAME, "has expected order of fields"
            else
                print FILENAME, "has extra fields"  
                next              
                }' key f{1..3}

打印:

f1 has expected order of fields
f2 order of Score is not correct
f3 order of Score is not correct

【讨论】:

  • 非常感谢你们的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多