我需要比较我的 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 '
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.