Eng7
Eng7

Reputation: 620

Multi-input files for awk

I have two CSV files, the first one looks like below:

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

File2:

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,,,,

what I need: if the $2 in file2 = 4001, then has to match $1 of file2 with file1, if $18 in file1 = 1106 for the matched $1 then print that line.

the expected output:

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

I have tried something as the following, but with no success.

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

P.S: The files are compressed, so I have to use the zcat command

Upvotes: 2

Views: 180

Answers (1)

jas
jas

Reputation: 10865

I would try something like:

$ 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

Upvotes: 4

Related Questions