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!

Replacing data from one file into another 1

Status
Not open for further replies.

learningawk

Technical User
Oct 15, 2002
36
US
I have found some similar posts but not quite like my problem.

I have a data file 1 that looks like:
00001 0 000 xxxxx yyyy
...numerous records of data
00020 0 000 xxxxx yyyy
...numerous records of data
00033 0 000 xxxxx yyyy
...numerous records of data

then I have a file 2 that looks like

00001 0 000 xxxxx yyyy
00020 0 000 xxxxx yyyy
00033 0 000 xxxxx yyyy
....

I want to replace the x and y values in file 1 with the x and y values from file 2. The records from both files have a similar field 1 which is sequential NR record.

I'm sure I have to use an array but not sure how to go about it.
Thanks
 
This assumes that field 1 of "...numerous records of data" will never match any field 1 in file2 and is untested.

awk -v fn="file2" -f la.awk file1

# ------ la.awk ------
BEGIN {
if (!fn) fn = "file2"
while ((getline < fn) > 0) {
xa($1) = $4
ya($1) = $5
}
}
{
if (xa($1)) {
$4 = xa($1)
$5 = ya($1)
}
print
}

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Sorry, I slipped back into fortran for a moment and used prarens instead of brackets for subscripts.

BEGIN {
if (!fn) fn = &quot;file2&quot;
while ((getline < fn) > 0) {
xa[$1] = $4
ya[$1] = $5
}
}
{
if (xa[$1]) {
$4 = xa[$1]
$5 = ya[$1]
}
print
}


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top