【问题标题】:awk to add header to output fileawk 将标题添加到输出文件
【发布时间】:2016-10-04 00:53:24
【问题描述】:

我正在尝试使用另一个filter_file 过滤file_to_filter,这只是$1 中的字符串列表。我想我很接近,但似乎无法在输出中包含标题行。 file_to_filter 也是制表符分隔的。谢谢你:)。

file_to_filter

Chr Start   End Ref Alt Func.refGene    Gene.refGene
chr1    160098543   160098543   G   A   exonic  ATP1A2
chr1    172410967   172410967   G   A   exonic  PIGC

filter_file

PIGC

所需的输出(包括标题)

Chr Start   End Ref Alt Func.refGene    Gene.refGene
chr1    172410967   172410967   G   A   exonic  PIGC

awk 与当前输出(不包括标题)

awk -F'\t' 'NR==1{A[$1];next}$7 in A' file test

chr1    172410967   172410967   G   A   exonic  PIGC

【问题讨论】:

    标签: awk


    【解决方案1】:

    假设您的字段确实是制表符分隔的:

    awk -F'\t' 'NR==FNR{tgts[$1]; next} (FNR==1) || ($7 in tgts)' filter_file file_to_filter
    

    要开始学习 awk,请阅读 Arnold Robbins 所著的《Effective Awk Programming, 4th Edition》一书。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      相关资源
      最近更新 更多