user3746195
user3746195

Reputation: 346

AWK: Basic Line by Line Matching and Field Changing

I cannot seem to figure out why this won't work. Please help. I'm trying to match the value from $7 in file1 into $7 in file2. I can get this to work when I copy the value from $7 in file1 into $3 in file2 but for some reason AWK doesn't like what I'm doing with $7. AWK only please.

Code

awk 'BEGIN {FS=OFS="\t"} 
       NR == FNR { values[substr($1,0,10) FS $2] = $7; next } 
       (substr($5,0,10) FS $6) in values { $7 = values[substr($1,0,10) FS $2]}1' file1 file2

File 1

LANGBPRJ01.RD.LA    ae31.0  25      400000.0    Default 25          

File2

RSMTDSRJ02  ae4.0   null    110 LANGBPRJ01  ae31.0  null    110

Output I see:

RSMTDSRJ02  ae4.0   null    110 LANGBPRJ01  ae31.0      110

Desired Output

RSMTDSRJ02  ae4.0   null    110 LANGBPRJ01  ae31.0  25  110

Upvotes: 1

Views: 46

Answers (1)

Ed Morton
Ed Morton

Reputation: 203189

$7 = values[substr($1,0,10) FS $2] should be $7 = values[substr($5,0,10) FS $6]

btw fields, strings, arrays, etc. all start at 1 in awk, not 0, so when you write substr(string,0,... that 0 in the 2nd parameter is simply an invalid value and awk treats any invalid value in that context as if it were 1. You could replace 0 with "aardvark" and get the same result. You should be using substr(string,1,... instead.

You probably don't actually need the substr()s on $5 of the 2nd file at all since it appears to always be 10 chars.

Finally - use , as the array index separator by default, just branch out to FS, OFS, or whatever if you have a specific need,

Try this:

awk 'BEGIN {FS=OFS="\t"} 
     NR == FNR { values[substr($1,1,10),$2] = $7; next } 
     ($5,$6) in values { $7 = values[$5,$6] } 1' file1 file2

Upvotes: 2

Related Questions