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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging 2 files with AWK 1

Status
Not open for further replies.

mrr

Technical User
May 3, 2001
67
US
I need help on merging 2 input files into one combined output.
Here's my input data file:


1111 100 sdfsdf sdfsdf etc.
1111 101 sdfsdf sdfsdf etc.
1111 102 sdfsdf sdfsdf etc.
1111 103 sdfsdf sdfsdf etc.
1111 110 sdfsdf sdfsdf etc.
1111 999 sdfsdf sdfsdf etc.
2222 0 sdfsdf sdfsdf etc.
2222 1 sdfsdf sdfsdf etc.
2222 10A sdfsdf sdfsdf etc.
3333 1 sdfsdf sdfsdf etc.

Here's my control file that is comma delimited:

aaaa,1111,100,999
bbbb,2222,0,10A
cccc,3333,1,1

and here's the combined output: space delimited

aaaa 1111 100 sdfsdf sdfsdf
aaaa 1111 101 sdfsdf sdfsdf
aaaa 1111 102 sdfsdf sdfsdf
aaaa 1111 103 sdfsdf sdfsdf
aaaa 1111 110 sdfsdf sdfsdf
aaaa 1111 999 sdfsdf sdfsdf
bbbb 2222 0 sdfsdf sdfsdf
bbbb 2222 1 sdfsdf sdfsdf
bbbb 2222 10A sdfsdf sdfsdf
cccc 3333 1 sdfsdf sdfsdf

All I need the script to do is to check for the similarity on the 3 fields in the control file and match with the first 2 fields of the data file and then append the first field of the control file to the data file.

The control file is a range of begining and end points for each group or it could be a single point, as in the case of cccc,3333,1,1

Thanks
 
I don't quite understand what you mean by a range of beginning and end points (the bbbb range is from 0 to 10A?), but here is something which should get you started.

BEGIN {
if (!fn) fn="control.dat"
while ((getline < fn) > 0) {
split($0,b,&quot;,&quot;)
a[b[2]]=b[1]
}
}
{
print a[$1],$0
} CaKiwi
 
Thanks CaKiwi,

Works great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top