Linux Awk-两个文件之间的匹配值

Linux Awk-两个文件之间的匹配值,linux,shell,awk,Linux,Shell,Awk,我尝试比较两个文件,并创建一个最终的.txt文件,其中包含两个文件中存在的数据 File1-column 1和File2-column 2包含两个文件之间需要匹配的值 因此,本质上,我试图->从文件1中获取column1,如果文件2的column2中存在匹配项,则将File1Column1、File1Column2和File2Column1写入名为final.txt的新文件 范例 文件1 1000,Brian 1010,Jason 400,Nick 文件2 3044 "1000&qu

我尝试比较两个文件,并创建一个最终的.txt文件,其中包含两个文件中存在的数据

File1-column 1和File2-column 2包含两个文件之间需要匹配的值

因此,本质上,我试图->从文件1中获取column1,如果文件2的column2中存在匹配项,则将File1Column1、File1Column2和File2Column1写入名为final.txt的新文件

范例

文件1

1000,Brian
1010,Jason
400,Nick
文件2

3044 "1000"
4466 "400"
1206 "1010"
输出文件看起来像

1000,Brian,3044
1010,Jason,1206
400,Nick,4466
我的测试代码没有显示任何结果

awk-F“[,]”NR==FNR{a[$1]=1“,”$2;next}($2 in a){print a[$2]”,“$1}”file1.txt file2.txt

我相信我应该能够用awk做到这一点,但出于某种原因,我真的很难做到这一点。任何帮助都将不胜感激


谢谢

能否请您尝试使用GNU
awk
中显示的样本编写并测试以下内容

awk '
FNR==NR{
  gsub(/"/,"",$2)
  arr[$2]=$1
  next
}
FNR==1{
  FS=","
  OFS=","
  $0=$0
}
($1 in arr){
  print $0,arr[$1]
}
'  Input_file2  Input_file1
说明:添加上述内容的详细说明

awk '                ##Starting awk program from here.
FNR==NR{             ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
  gsub(/"/,"",$2)    ##globally substituting " in 2nd field with NULL.
  arr[$2]=$1         ##Creating array arr with index of 2nd field and value of 1st field.
  next               ##next will skip all further statements from here.
}
FNR==1{              ##Checking condition if this is first line of Input_file1.
  FS=","             ##Setting FS as comma here.
  OFS=","            ##Setting OFS as comma here.
  $0=$0              ##Reassigning current line to itself so that field separator values will be implemented to current line.
}
($1 in arr){         ##Checking condition if 1st field is present in arr then do following.
  print $0,arr[$1]   ##Printing current line and value of array arr.
}
' file2 file1        ##Mentioning Input_file names here.

您没有在示例输入/输出中包含任何两个输入文件之间不匹配的行,因此这可能会或可能不会满足您对这些情况的要求:

$ cat tst.awk
BEGIN { FS="[[:space:]\",]+"; OFS="," }
NR==FNR {
    map[$2] = $1
    next
}
{ print $0, map[$1] }

请不要以使答案无效的方式编辑问题。你之前已经接受了一个答案。不接受答案,然后更改/添加问题是对及时帮助您的用户的不尊重。请不要那样做。谢谢你。另请注意:添加的重复链接可以作为参考,但不是重复链接。
$ awk -f tst.awk file2 file1
1000,Brian,3044
1010,Jason,1206
400,Nick,4466