Awk 合并某些列匹配的文件

Awk 合并某些列匹配的文件,awk,Awk,匹配两个文件中的列1、2、3,如果它们相等,则匹配 对于列匹配的文件,请将文件1中第4列的值写入文件2 如果不匹配,则写NA file1 31431 37150 100 10100 31431 37201 100 12100 31431 37471 100 14100 文件2 31431 37150 100 14100 31431 37131 100 14100 31431 37201 100 14100 31431 37478 100 14100 31431 37471 100 14100

匹配两个文件中的列1、2、3,如果它们相等,则匹配

对于列匹配的文件,请将文件1中第4列的值写入文件2 如果不匹配,则写NA

file1

31431 37150 100 10100
31431 37201 100 12100
31431 37471 100 14100
文件2

31431 37150 100 14100
31431 37131 100 14100
31431 37201 100 14100
31431 37478 100 14100
31431 37471 100 14100
所需输出:

31431 37150 100 14100 10100
31431 37131 100 14100 NA
31431 37201 100 14100 12100
31431 37478 100 14100 NA
31431 37471 100 14100 14100
我试过了

awk '
FNR==NR{
  a[$1 $2 $3]=$4
  next
}
($1 in a){
  $1=a[$1]
  found=1
}
{
  $0=found==1?$0",":$0",NA"
  sub(/^...../,"&,")
  $1=$1
  found=""
}
1
' FS=" " file1 FS=" " OFS="," file2

你能试试下面的吗

awk 'FNR==NR{a[$1,$2,$3]=$NF;next} {print $0,($1,$2,$3) in a?a[$1,$2,$3]:"NA"}' Input_file1  Input_file2
或者根据Ed sir的评论为字段创建变量

awk '{var=$1 OFS $2 OFS $3} FNR==NR{a[var]=$NF;next} {print $0,var in a?a[var]:"NA"}' Input_file1  Input_file2
输出如下

31431 37150 100 14100 10100
31431 37131 100 14100 NA
31431 37201 100 14100 12100
31431 37478 100 14100 NA
31431 37471 100 14100 14100
解释:现在为上述代码添加解释

awk '
{
  var=$1 OFS $2 OFS $3              ##Creating a variable named var whose value is first, second ansd third field of current lines of Input_file1 and Input_file2.
}
FNR==NR{                            ##Checking condition FNR==NR which will be TRUE when first Input_file1 is being read.
  a[var]=$NF                        ##Creating an array named a whose index is variable var and value is $NF of curent line.
  next                              ##next keyword will skip all further lines from here.
}
{
  print $0,var in a?a[var]:"NA"     ##Printing current line value and along with that printing either value of a[var] or NA based upon if var present in array a then print a[var] else print NA.
}'  Input_file1  Input_file2        ##Mentioning Input_file names here.

FS=”“file1 FS=”“OFS=“,”file2
中设置FS时,您试图做什么?您正在将OFS设置为
,并在代码中对逗号进行一些额外的硬编码,但预期输出中没有任何逗号的迹象。为什么?
awk '
{
  var=$1 OFS $2 OFS $3              ##Creating a variable named var whose value is first, second ansd third field of current lines of Input_file1 and Input_file2.
}
FNR==NR{                            ##Checking condition FNR==NR which will be TRUE when first Input_file1 is being read.
  a[var]=$NF                        ##Creating an array named a whose index is variable var and value is $NF of curent line.
  next                              ##next keyword will skip all further lines from here.
}
{
  print $0,var in a?a[var]:"NA"     ##Printing current line value and along with that printing either value of a[var] or NA based upon if var present in array a then print a[var] else print NA.
}'  Input_file1  Input_file2        ##Mentioning Input_file names here.