AWK Compare files, different fields, output

Hi All,

Looking for a quick AWK script to output some differences between two files.

FILE1

device1                    1.1.1.1                          PINGS
device1                    2.2.2.2                         PINGS

FILE2
2862 SITE1 device1-prod 1.1.1.1 icmp - 0
2862 SITE2 device2-prod 1.1.1.1 icmp - 0

OUTPUT_FILE

device1                    1.1.1.1                          PINGS

Basically column two in FILE1 needs to match in Column 4 in FILE2 and output the match from FILE1 in the output file.

My Script at the moment is

awk 'NR==FNR{++a[$4];next}!a[$2]' FILE2 FILE1 > OUTPUT_FILE

But this gives me
2862 SITE1 device1-prod 1.1.1.1 icmp - 0

Can someone help refine the AWK command.

Thanks all

Stacky

try this,

awk 'NR==FNR {a[$4]++;next} a[$2]' file2 file1

Abolsutley perfect. Thanks.

How about displaying the missing differences.

i.e.
OUTPUT_FILE
device1 2.2.2.2 PINGS

where 2.2.2.2 is not in FILE2 at all

Thanks again

for missing..

awk 'NR==FNR {a[$4]++;next} !a[$2]' file2 file1

ACE!!! Thanks bud

Worked a treat. exclamation eh!!! should have checked that one out myself :slight_smile: