awk的多输入文件

时间:2015-07-23 09:33:42

标签: linux unix awk

我有两个CSV文件,第一个看起来如下:

File1中:

3124,3124,0,2,,1,0,1,1,0,0,0,0,0,0,0,0,1106,11
6118,6118,0,0,,0,0,1,0,0,0,0,1,1,1,1,1,5156,51
6679,6679,0,0,,1,0,1,0,0,0,0,0,1,0,1,0,1106,11
5249,5249,0,0,,0,0,1,1,0,0,0,0,0,0,0,0,1106,13
2658,2658,0,0,,1,0,1,1,0,0,0,0,0,0,0,0,1197,11
4322,4322,0,0,,1,0,1,1,0,0,0,0,0,0,0,0,1307,13

文件2:

7792,1307,2012-06-07,,,,
5249,4001,2016-07-02,,,,
6001,1334,2017-01-23,,,,
2658,4001,2009-02-09,,,,
9279,1326,2014-12-20,,,,

我需要什么: 如果 file2 中的$2 = 4001,则必须将 file2 $1 file1 匹配,如果匹配的$18 file1 = 1106$1,则打印该行。

预期产出:

5249,5249,0,0,,0,0,1,1,0,0,0,0,0,0,0,0,1106,13

我尝试了以下内容,但没有成功。

awk 'NR=FNR {A[$1]=$1;next} {print $1}'

P.S:文件已压缩,因此我必须使用zcat命令

1 个答案:

答案 0 :(得分:4)

我会尝试类似的事情:

$ cat t.awk
BEGIN { FS = "," }

# Processing first file
NR == FNR && $18 == 1106 { a[$1] = $0; next }

# Processing second file
$2 == 4001 && $1 in a { print a[$1] }


$ awk -f t.awk file1.txt file2.txt
5249,5249,0,0,,0,0,1,1,0,0,0,0,0,0,0,0,1106,13