【问题标题】:match one column to another column of another file unix将一列与另一个文件的另一列匹配 unix
【发布时间】:2019-11-19 03:04:49
【问题描述】:

我有file_1

BlockID
VT11742
VT11744
VT11050
VT11870
VT12147
VT12149
VT12176
VT12180

和文件_2

AB Ref2        VICTOR 
crc_31-C1        VT11929
crc_31-C2     VT11929 C2
crc_31-N VT11929 NORMAL
crc_32-C1        VT11050
crc_32-C2     VT11050 C2
crc_33-C1        VT11656
crc_33-C2     VT11656 C2
crc_33-N VT11656 NORMAL
crc_34-C1        VT11981
crc_34-C2     VT11981 C2

我想要的输出是当 file1 的第一列与 file2 匹配时打印出 file2 的第一列,并以与 file2 相同的顺序打印出来,并在“不匹配”时打印

输出

BlockID
VT11742 no_match
VT11744 no_match
VT11050 crc_32-C1
VT11870 no_match
VT12147 no_match
VT12149 no_match
VT12176 no_match
VT12180 no_match

我以为我可以做这样的事情 grep -Ff file1 file2 > 输出

【问题讨论】:

  • 您说您想要print out in the same order of file2,但这不是您在预期输出中显示的内容。请修正您的问题以保持一致/准确。还要在file1 中包含VT11929,这样当file2 中有多个匹配项时,我们可以看到预期的输出。

标签: unix join awk grep pattern-matching


【解决方案1】:

您能否尝试以下操作(使用您展示的示例编写和测试)。

awk 'FNR==NR{a[$2]=$1;next} FNR>1{$0=$0 OFS ($1 in a?a[$1]:"no_match")} 1'  file_2  file_1

输出如下。

BlockID
VT11742 no_match
VT11744 no_match
VT11050 crc_32-C2
VT11870 no_match
VT12147 no_match
VT12149 no_match
VT12176 no_match
VT12180 no_match

【讨论】:

    【解决方案2】:

    print out in the same order of file2 如您(但未在您的示例中显示)您想要的:

    $ awk 'NR==FNR{if (NR==1) print; else a[$1]; next} FNR>1{print $2, ($2 in a ? $1 : "no_match")}' file1 file2
    BlockID
    VT11929 no_match
    VT11929 no_match
    VT11929 no_match
    VT11050 crc_32-C1
    VT11050 crc_32-C2
    VT11656 no_match
    VT11656 no_match
    VT11656 no_match
    VT11981 no_match
    VT11981 no_match
    

    【讨论】:

      【解决方案3】:

      这是另一个awk 脚本。对于不明确的要求。希望它对你有用。

      script.awk

      FNR == NR { # process file 1 only
          keysArr[$2] = $1; # read each 2nd field into array
          next;   # skip any further handling
      }
      FNR > 1{    # process file 2 form line 2
          $2 = "no match";   # set default match result
          if ($1 in keysArr) { # match key in file 1
               $2 = keysArr[$1]; # overwrite 2nd output field
          }
      }
      1           # ouput everything computed for file 2
      

      input.1.txt

      BlockID
      VT11742
      VT11744
      VT11050
      VT11870
      VT12147
      VT12149
      VT12176
      VT12180
      

      input.2.txt

      AB Ref2        VICTOR
      crc_31-C1        VT11929
      crc_31-C2     VT11929 C2
      crc_31-N VT11929 NORMAL
      crc_32-C1        VT11050
      crc_32-C2     VT11050 C2
      crc_33-C1        VT11656
      crc_33-C2     VT11656 C2
      crc_33-N VT11656 NORMAL
      crc_34-C1        VT11981
      crc_34-C2     VT11981 C2
      

      运行:

      awk -f script.awk input.2.txt input.1.txt
      

      输出:

      BlockID
      VT11742 no match
      VT11744 no match
      VT11050 crc_32-C2
      VT11870 no match
      VT12147 no match
      VT12149 no match
      VT12176 no match
      VT12180 no match
      

      【讨论】:

        猜你喜欢
        • 2015-08-07
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多