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

REPLACE 2 FIELDS FROM ANOTHER FILE 1

Status
Not open for further replies.

mrr

Technical User
May 3, 2001
67
US
Is there a simple awk script to do the following?

I have a data file 1 such as:
FIELD1,FIELD2A,100,1000,10000
FIELD1,FIELD2A,101,2222,33333
FIELD1,FIELD2A,133,44444,5555
FIELD1,FIELD2,400,1,1
FIELD1,FIELD2E,166,1,1

and file 2 is like:
FIELD__1 FIELD2A 100 0.0 0.0 0.000000 1475
FIELD__1 FIELD2A 100 0.0 0.0 1.028590 1482
FIELD__1 FIELD2A 100 0.0 0.0 1.168080 1491
FIELD__1 FIELD2A 101 0.0 0.0 1.217630 1511
FIELD__1 FIELD2A 133 0.0 0.0 0.000000 1475
FIELD__1 FIELD2A 133 0.0 0.0 1.061950 1488
FIELD__1 FIELD2A 133 0.0 0.0 3.103750 2220
FIELD__1 FIELD2A 133 0.0 0.0 3.415240 2447
FIELD__1 FIELD2B 133 0.0 0.0 3.876910 2666
FIELD__1 FILED2C 133 0.0 0.0 4.153300 2890
FIELD__1 FIELD2D 133 0.0 0.0 5.156230 3773
FIELD__1 FIELD2E 166 0.0 0.0 0.000000 1475

I want to read file 1 fields 2 and 3 and if they are the same as file 2 fields 2 and 3 then I want to update file 2 fields 4 and 5 with values from file 1 fields 4 and 5.

Thanks for any assistance.
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV for responding.
Here's what I have but it is not looping through the control file like it should. It is getting the last xy pair and not for each record that matches on fields 2 and 3.

BEGIN{
if (!fn) fn= "test_xy_input.dat"
while ((getline < fn) > 0) {
split($0,b," ")
}
}
{
if ($2 == b[2] && $3 = b[3]) {x = b[4];y= b[5]}
printf("%-9s %-20s %6d %12.1f %12.1f %7.6f %5d\n",$1,$2,$3,x,y,$6,$7)
}
 
The problem there was that you were trying to use the same b array for every input line of file1, so each time it overwrote b leaving you with only the last line. You need to use arrays with appropriate subscripts to store the entire contents of file1 before processing file2.

Try this:

Code:
BEGIN{
        FS=","          # temporarily change field separator
        fn="file1"
        while (getline < fn) {
                field4array[$2,$3]=$4
                field5array[$2,$3]=$5
        }
        close(fn)       # a good habit to get into
        FS=" "
}
(($2,$3) in field4array) {
        # override the values of fields 4 and 5 if the
        # indexes exist in field4array
        $4=field4array[$2,$3]
        $5=field5array[$2,$3]
}
{
        # print every line
        printf("%-9s %-20s  %6d  %12.1f  %12.1f  %7.6f  %5d\n",
                $1,$2,$3,$4,$5,$6,$7)
}

Let us know if you need any further explanation.

Annihilannic.
 
Annihilannic,

Your script worked perfect.
Thank You
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top