我有两个文件要进行比较,并用这两个文件中的数据创建一个final.txt文件。
File1-列1和File2-列2包含我需要在这两个文件之间匹配的值。
因此,本质上,我试图->从文件1中取出第1列,如果文件2的第2列匹配,则将文件1列1、文件1列2和文件2列1写入名为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做到这一点,但出于某种原因,我真的很难对付这个。如有任何帮助,我们将不胜感激。
感谢
请您尝试以下内容,用GNUawk
中显示的示例编写并测试。
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.
您没有在示例输入/输出中包含任何与2个输入文件不匹配的行,因此这可能会也可能不会满足您的要求:
$ 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