【问题标题】:Compare two columns of two files, print the row if it matches and print zero in third column比较两个文件的两列,如果匹配则打印该行,并在第三列打印零
【发布时间】:2021-06-26 03:27:50
【问题描述】:

我需要比较我的 file1.txt 和 file2.txt 的第 1 列和第 2 列。如果两列都匹配,则打印 file1.txt 的整行,但如果 file1.txt 中的一行在 file2.txt 中不存在,则还要在输出中打印缺失的行并在第三列中添加“0”作为其值。

#file1.txt #

AA ZZ   
JB  CX
CX  YZ  
BB  XX
SU  BY  
DA  XZ  
IB  KK  
XY  IK
TY  AB

#file2.txt #

AA ZZ   222
JB  CX  345
BB  XX  3145
DA  XZ  876
IB  KK  234
XY  IK  897

预期输出 # output.txt #

File1.txt
AA ZZ   222
JB  CX  345
CX  YZ  0
BB  XX  3145
SU  BY  0
DA  XZ  376
IB  KK  234
XY  IK  897
TY  AB  0

我尝试了这段代码,但不知道如何添加不匹配的行并将"0" 添加到其中

awk 'BEGIN { while ((getline <"file2.txt") > 0) {REC[$1]=$0}}{print REC[$1]}' < file1.txt > output.txt

【问题讨论】:

    标签: awk


    【解决方案1】:

    你可以试试这个awk:

    awk '
    FNR == NR {
       map[$1,$2] = $3
       next
    }
    {
       print $1, $2, (($1,$2) in map ? map[$1,$2] : 0)
    }' file2 file1
    
    AA ZZ 222
    JB CX 345
    CX YZ 0
    BB XX 3145
    SU BY 0
    DA XZ 876
    IB KK 234
    XY IK 897
    TY AB 0
    

    【讨论】:

      【解决方案2】:
      $ awk '
          { key = $1 FS $2 }
          NR==FNR { map[key]=$3; next }
          { print $0, map[key]+0 }
      ' file2.txt file1.txt
      AA ZZ 222
      JB  CX 345
      CX  YZ 0
      BB  XX 3145
      SU  BY 0
      DA  XZ 876
      IB  KK 234
      XY  IK 897
      TY  AB 0
      

      【讨论】:

        【解决方案3】:

        对于您展示的示例,您能否尝试以下操作。

        awk '
        FNR==NR{
          arr[$1 OFS $2]
          next
        }
        (($1 OFS $2) in arr){
          print
          arr1[$1 OFS $2]
        }
        END{
          for(i in arr){
            if(!(i in arr1)){
              print i,0
            }
          }
        }
        ' file1.txt file2.txt
        

        说明:为上述添加详细说明。

        awk '                    ##Starting awk program from here.
        FNR==NR{                 ##Checking FNR==NR condition which will be TRUE when file1.txt is being read.
          arr[$1 OFS $2]         ##Creating array with 1st and 2nd field here.
          next                   ##next will skip all further statements from here.
        }
        (($1 OFS $2) in arr){    ##Checking condition if 1st and 2nd field of file2.txt is present in arr then do following.
          print                  ##Print the current line here.
          arr1[$1 OFS $2]        ##Creating array arr1 with index of 1st and 2nd fields here.
        }
        END{                     ##Starting END block of this program from here.
          for(i in arr){         ##Traversing through arr all elements from here.
            if(!(i in arr1)){    ##Checking if an element/key is NOT present in arr1 then do following.
              print i,0          ##Printing index and 0 here.
            }
          }
        }
        ' file1.txt file2.txt    ##Mentioning Input_file names here.
        

        【讨论】:

          猜你喜欢
          • 2021-10-30
          • 2016-10-23
          • 2020-11-24
          • 1970-01-01
          • 2017-07-30
          • 1970-01-01
          • 2014-08-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多