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 fields and print to separate file

Status
Not open for further replies.

rhnaeco

Technical User
Aug 11, 2005
45
I have two files both with a structure like

12.7244 45.4828 -16.099
12.7249 45.4985 -12.45
12.7253 45.4888 -14.797
12.7257 45.5045 -11.79
12.7261 45.4948 -13.767
12.7265 45.4851 -15.631

I would like awk/sed to search the lines of one file and if $1 and $2 match with $1 and $2 in the other file. If they match i would like it to print to a third file, $0 plus $3 of the other file.
e.g.
(12.7265 45.4851 -15.631 -12.45)


Reading through the other posts i have figured out the first part, but not to print $3 on the same line


awk -F: '
BEGIN {
while (getline < "file1.dat") {
long[$1]=1
}
}
{
if (long[$1])
print $0
}
' file2.dat > output.dat

Thanks in advance

rhnaeco
 
You may try this:
awk -F: '
BEGIN {
while (getline < "file1.dat") {
long[$1,$2]=$3
}
}
{
if (long[$1,$2])
print $0, long[$1,$2]
}
' file2.dat > output.dat

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It took a while to realise why it wasn't working, but once i took out the -F: field separator (teach me to copy and paste!), it worked like a charm, Thanks again PHV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top