简体   繁体   English

比较awk中的两列,并将查找文件中的值打印到输出文件中

[英]compare two columns in awk and print values from lookup files into output file

I have two files with first file having ~16000 files and the second file is lookup file having ~4000 lines. 我有两个文件,第一个文件有~16000个文件,第二个文件是查找文件有~4000行。

Sample contents of file1 is given below: file1的示例内容如下:

id,title,name,value,details
01,23456,   ,   ,abcdefg
02,23456,   ,   ,abcdefg
03,12345,   ,   ,abcdefg
04,34534,   ,   ,abcdefg
...

Sample contents of lookup file file2 is given below: 查找文件file2的示例内容如下:

sno,title,name,value
1,23456,abc,xyz
2,12345,cde,efg
3,34534,543,234

Now my requirement is compare column 2 of file1 in the lookup file and insert the values of column3 and column4 from lookup file into new output file. 现在我的要求是比较查找文件中file1的第2列,并将查询文件中column3和column4的值插入到新的输出文件中。

The output file should look like below: 输出文件应如下所示:

id,title,name,value,details
01,23456,abc,xyz,abcdefg
02,23456,abc,xyz,abcdefg
03,12345,cde,efg,abcdefg
04,34534,543,234,abcdefg

I did try few iterations by looking at existing questions but didn't get the results I desired. 我通过查看现有问题尝试了几次迭代,但没有得到我想要的结果。 Any solution with awk would be much helpful. 使用awk的任何解决方案都会非常有用。

$ cat vino.awk
BEGIN { FS = OFS = "," }
NR==FNR { name[$2]=$3; value[$2]=$4; next }
{ print $1, $2, name[$2], value[$2], $5 }

$ cat file1
id,title,name,value,details
01,23456,   ,   ,abcdefg
02,23456,   ,   ,abcdefg
03,12345,   ,   ,abcdefg
04,34534,   ,   ,abcdefg

$ cat file2
sno,title,name,value
1,23456,abc,xyz
2,12345,cde,efg
3,34534,543,234

$ awk -f vino.awk file2 file1
id,title,name,value,details
01,23456,abc,xyz,abcdefg
02,23456,abc,xyz,abcdefg
03,12345,cde,efg,abcdefg
04,34534,543,234,abcdefg

Here's an awk oneliner: 这是一个awk oneliner:

awk -F, 'FNR==NR {n[$2]=$3;v[$2]=$4} FNR!=NR{OFS=","; print $1,$2,n[$2],v[$2],$5}' file2 file1

The idea is to process in two passes, first for file2 to store all of the names and values, then for file1, to print out each line including the collected names and values. 我们的想法是两次处理,首先是file2存储所有的名称和值,然后是file1,打印出每一行,包括收集的名称和值。

awk -F"," 'BEGIN{OFS=","} NR==FNR {a[$2]=$3","$4;next} {print $1,$2,a[$2],$5;}' file2 file1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM