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 merge two files

Status
Not open for further replies.

rkdash

Technical User
Jul 15, 2004
14
US
Hello all,
I have two files:

file1:
428165.27 5356169.59 128.20 41074
428071.74 5356209.69 128.10 41075
427978.10 5356239.79 128.00 41076
427885.32 5356279.88 127.90 41077
427790.93 5356309.99 127.80 41078
427696.68 5356350.10 127.60 41079

file2:

41074.0 -10.730 2.151377
41075.0 -10.811 2.166259
41077.0 -10.977 2.183353
41078.0 -11.060 2.198520
41079.0 -11.143 2.213687


I want to write a file which will
i.) Compare 4th column of "file1" with first column of "file2",
ii.)Ignore the unmatched lines of "file1"
and
iii.) Print the output in a separate file with the third column of "file2" as the fifth column of "file1".

So my output should look like this:

428165.27 5356169.59 128.20 41074 2.151377
428071.74 5356209.69 128.10 41075 2.166259
427885.32 5356279.88 127.90 41077 2.183353
427790.93 5356309.99 127.80 41078 2.198520
427696.68 5356350.10 127.60 41079 2.213687


Thanks in advance.

rkdash
 
Something like this ?
awk '
BEGIN{while((getline<"file2")>0)a[$1+0]=$3}
{if($4+0 in a)print $0,a[$4+0]}
' file1 >"separate file"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top