awk to compare 2nd and 3rd field and print the differences

need a one liner to compare 2nd and 3rd field and print values that are not matched in 2nd field

Input

col 2    col 3
1.1.1.1  11.11.11.11
8.8.8.8   0.0.0.0
3.3.3.3   2.2.2.2
7.7.7.7   3.3.3.3
5.5.5.5   1.1.1.1
          4.4.4.4
          6.6.6.6
          9.9.9.9

output

7.7.7.7 
5.5.5.5 
8.8.8.8 

Please help me out

P.S. Both colum length differ and there are around 1000+ records

Without seeing your actual data, this will print column2 if it is not equal to column3:

awk '{if($2!=$3){print $2}}' filename

Hi ,

That would work only when values to be compared fall in the same row right.!

Correct.

mine is different.. the values are scattered randomly

Try:

awk 'NR==FNR{A[$3]; next} !($2 in A) && NF>2 && FNR>1 {print $2}' infile infile

@Scurtinizer

Thanks for the response. But my input is one single file where i have to compare 2nd and 3rd field and print values of 2nd field that are not in 3rd field

Yes, the same input file needs to gets read twice and is therefore specified twice on the command line...

@ Scuritizer.. Thanks it looks fine. i will run it on the entire record and let you know .
does this one liner ignore the first line of the file. ( i guess you have omitted it keeping the column header in mind )

awk -f a.awk infile

Where a.awk:

NR==1 {
 p1=index($0,"col 2");
 p2=index($0,"col 3");
}
NR > 1 {
 v1=substr($0, p1, p2-1);
 sub("^ *", "", v1);
 sub(" *$", "", v1);
 if (length(v1)>0) a[ac++]=v1;
 v2=substr($0, p2);
 sub("^ *", "", v2);
 sub(" *$", "", v2);
 if (length(v2)>0) b[v2]=v2;
}
END {
 for (i=0; i<ac; i++) if (length(b[a])<1) print a;
}

Yes, && FNR>1 keeps the value on the first line of the file from being printed. You can leave that out if the actual file does not contain a header...

1 Like

@scrutinzer
Thanks for your inputs.. can you please explain the oneliner :smiley: