2

Kindly consider below files:

File 1:

boo,194,2322
foo,999,7559

File 2:

boo,2322
boo,4526
foo,4222
foo,4223

I need to link Field1 in File 1 with Field1 in File 2 and get the related Field2 from File 2, while excluding the result if it's equal to Field3 in File 1.

The result should be:

boo,4526
foo,4222,4223

I tried the below script, but it does not exclude the similar values.

awk -F, 'NF==3{arr[$1]=$3}{if(arr[$1]==$1){print $2}}'
  • 1
    @RuiFRibeiro your comment isn't helpful anyway! – Eng7 Nov 15 '15 at 12:57
  • You need to check whether $1 is in arr and if so whether the value is different from $2 and then print, don't you? – Jonathan Leffler Nov 15 '15 at 15:10
  • @JonathanLeffler Yes sir. – Eng7 Nov 15 '15 at 15:11
2

You need to check whether $1 is in arr and if so whether the value is different from $2 and then print:

awk -F, 'FNR == NR { arr[$1] = $3; next }
         { if ($1 in arr && arr[$1] != $2) print $2 }'

Using FNR == NR and next is the conventional way to process lines in the first file differently from the lines in other files. Yes, you can flatten it onto one line, but 'one-liner' is a pejorative term unless you're writing APL (or perhaps Perl).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.