2

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

2
  • If you are using bash, you can unzip the files and pass them in separately using "process substitution", like this...awk '{...}' <(zcat file1.Z) <(zcat file2.Z). And you need FNR==NR{} with double equals. Commented Jul 23, 2015 at 9:43
  • @MarkSetchell, Thanks for the info, that solve the compression issue ... Commented Jul 23, 2015 at 9:45

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.