compare two columns of different files and print the matching second file..

Hi,

I have two tab separated files;

file1:

S.No ddi fi cu o/l t+ t-
1 0.5 0.6 o 0.1 0.2
2 0.2 0.3 l 0.3 0.4
3 0.5 0.8 l 0.1 0.6

file2:

S.No ddi fi cu o/l t+ t-
1 0.8 0.9 o 0.5 0.6
2 0.5 0.2 o 0 0
3 0.2 0.3 l 0 0
4 0.5 0.6 l 0 0
5 0.25 0.32 o 0.3 0.6

Desired output:
S.No ddi fi cu o/l t+ t-
1 0.8 0.9 o 0.5 0.6
2 0.5 0.6 o 0.1 0.2
3 0.2 0.3 l 0.3 0.4
4 0.5 0.8 l 0.1 0.6
5 0.25 0.32 o 0.3 0.6

That is the first file's second column(Tab as feild separator) needs to be compared with all second file's second column, if match found print the first file's line, if match not found print second file's line.

Thanks in Advance,
Vasanth

---------- Post updated at 08:49 AM ---------- Previous update was at 08:35 AM ----------

anybody there to help me..

thanks in advance
vasanth

Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.

Ok noticed..

i will wait till anybody helps me..

Thanks,
Vasanth

awk 'NR == FNR { A[$2]=$0; next } { if (A[$2]) print A[$2]; else print }' file1 file2
S.No ddi fi cu o/l t+ t-
1 0.8 0.9 o 0.5 0.6
3 0.5 0.8 l 0.1 0.6
2 0.2 0.3 l 0.3 0.4
3 0.5 0.8 l 0.1 0.6
5 0.25 0.32 o 0.3 0.6

Shorter :wink:

awk 'NR==FNR{a[$2]=$0;next}{$0=a[$2]?a[$2]:$0}1' file1 file2

Even shorter :wink:

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