我有两个文件:$wrkfile
和$mapfile
。我需要查看$ wrkfile和$ 5 $ ==" 2R"然后我需要从$ mapfile中将$ 2的新费率拉出来并将其写入$ wrkfile中的5美元。如果$ 5 =" 2R"然后什么也不做,接下来。
以下是$wrkfile
,$mapfile
和$expected
的示例。我还包括我正在使用的awk
脚本失败。任何帮助将不胜感激。
awk -F"|" 'NR==FNR{a[$1]=$2} NR>FNR{ if ( $5 == "2R" ) {print($1,$2,$3,$4,a[$1],$6} else
{print($1,$2,$3,$4,$5,$6} }' OFS="|" "$mapfile" "$wrkfile" > "$output"
$wrkfile
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|2R|000000050.00|
12345678912|C|01|A|2R|000000050.00|
12345678912|C|01|A|2R|000000050.00|
12345678912|C|01|A|2R|000000050.00|
12345678912|C|01|A|2R|000000050.00|
$mapfile
12345678912|9.00|
12345678914|10.00|
12345678993|11.00|
12345678983|12.00|
12345678963|13.00|
12345678917|14.00|
$expected
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|2R|000000009.00|
12345678912|C|01|A|2R|000000009.00|
12345678912|C|01|A|2R|000000009.00|
12345678912|C|01|A|2R|000000009.00|
12345678912|C|01|A|2R|000000009.00|
答案 0 :(得分:0)
我修改了一些输入条目以获得一些匹配,您的代码可以更改为
$ awk -F"|" -v OFS="|" '
NR==FNR{a[$1]=$2;next}
$3~/2R/{$6=a[$1]}
1' mapfile 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 |2R |A |01 |9.00 |
12345678912|C |2R |A |01 |9.00 |
12345678912|C |2R |A |01 |9.00 |
12345678912|C |2R |A |01 |9.00 |
12345678912|C |2R |A |01 |9.00 |
格式化最后一列是可能的,但我不确定双小数点是什么意思。
答案 1 :(得分:0)
给出新的输入和输出文件:
$ cat tst.awk
BEGIN { FS=OFS="|" }
NR==FNR { map[$1] = $2; next }
$5=="2R" { $6 = sprintf("%0*.2f",length($6),map[$1]) }
{ print }
$ awk -f tst.awk mapfile wrkfile
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|01|000000050.00|
12345678912|C|01|A|2R|000000009.00|
12345678912|C|01|A|2R|000000009.00|
12345678912|C|01|A|2R|000000009.00|
12345678912|C|01|A|2R|000000009.00|
12345678912|C|01|A|2R|000000009.00|