vendredi 2 octobre 2015

if statement within awk script

I have two files; $wrkfile and $mapfile. $wrkfile contains RATE and $mapfile contains NEW RATE. I want to scan through the $wrkfile and IF $5=="2R", find the EmplID in $mapfile, get the NEW RATE, and write the NEW RATE from $mapfile back to the RATE field in $wrkfile. Below is an an example if $wrkfile, $mapfile, and $expected. I have also included the awk script I am using that is failing. any help would be greatly appreciated.

awk -F"|" 'NR==FNR{a[$1]=$2} NR>FNR{ if ( $5 == "2R" ) {print($1,$2,$3,$4,a[$1]} else  
{print($1,$2,$3,$4,$5} }' OFS="|" "$mapfile" "$wrkfile" > "$output"


$wrkfile

EMPLID     |TYPE1|REC|TYPE2|CODE|RATE        |
12345678912|C    |01 |A    |01  |000000.50.00|
12345678912|C    |01 |A    |01  |000000.50.00|
12345678912|C    |01 |A    |01  |000000.50.00|
12345678912|C    |01 |A    |01  |000000.50.00|
12345678912|C    |01 |A    |01  |000000.50.00|
12345678912|C    |01 |A    |01  |000000.50.00|
12345678912|C    |01 |A    |01  |000000.50.00|
12345678912|C    |01 |A    |01  |000000.50.00|

$mapfile

EMPLID     |NEW RATE|
12345678912|9.00    |
12345678914|10.00   | 
12345678993|11.00   |
12345678983|12.00   | 
12345678963|13.00   |
12345678917|14.00   |

$wrkfile

EMPLID     |TYPE1|REC|TYPE2|CODE|RATE        |
12345678912|C    |01 |A    |01  |000000050.00|
12345678912|C    |01 |A    |01  |000000050.00|
12345678912|C    |01 |A    |01  |000000050.00|
12345678912|C    |2R |A    |01  |000000009.00|
12345678912|C    |2R |A    |01  |000000009.00|
12345678912|C    |2R |A    |01  |000000009.00|
12345678912|C    |2R |A    |01  |000000009.00|
12345678912|C    |2R |A    |01  |000000009.00|

Aucun commentaire:

Enregistrer un commentaire