Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare and select unmatched lines

Status
Not open for further replies.

rkdash

Technical User
Jul 15, 2004
14
US
Hi all,

I have two files for which the first column of file1 contains all the first column entries of file2 and some more. I want to write another file from file1 whose first column entries are not present in the first column of file2.

For example:

file1:
--------
9375 63.511 76.900 0.004 3.960 0.080 1
9376 63.522 76.906 0.004 3.962 0.080 1
9377 63.514 76.936 0.004 3.966 0.080 1
9378 63.537 76.924 0.004 3.967 0.080 1
9379 61.967 78.543 0.004 4.109 0.080 1
9380 61.951 78.542 0.004 4.108 0.080 1


file2:
-------
9379 61.967 78.543 0.004 -0.217 0.080 1
9380 61.951 78.542 0.004 -0.218 0.080 1


Desired file3:
--------------
9375 63.511 76.900 0.004 3.960 0.080 1
9376 63.522 76.906 0.004 3.962 0.080 1
9377 63.514 76.936 0.004 3.966 0.080 1
9378 63.537 76.924 0.004 3.967 0.080 1


Any help is appreciated.

rk
 
A starting point (typed, untested):
awk '{NR==FNR{++a[$1];next}!a[$1]}' file2 file1 > file3

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Sorry PHV, it doesn't work. Could you please check the syntax again ?

Thanks

rk

 
You might also consider using the join command if you have it - something like:

join file1 file2 -v2
 
Thanks feherke, it works.

taupirho, i had tried with join commnand (join -a1 file1 file2) before. It works for small files, but fails with the number of lines is large.

Is there any reason for that ?

rk
 
join requires the files to be sorted on the joined fields, that's most likely the cause.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top