A VAL|1|2|3|
C VAL|2|2|3|
D VAL|1|2|3|
[No space between lines]
I want to replace the values in the above as per the first col i.e A VAL,C
VAL,D VAL,
so I want to
1. replace 3 from A VAL row
2. replace 2 value from C VAL row.
3. replace 1 value from D VAL row.
Basically I want to modify the above values by using AWK as AWK helps
treating csv , pipe delimited files
So I tried by using AWK command as
enter code here
`awk 'BEGIN {OFS=FS="|"} {if ($1="A") sub($4,"A1") ;elseif ($1="C") sub
($2,"B1"); print }' myval.txt`
*但我得错了结果*
C ^ | B1 | 2 | A1 | B1C
C ^ | B1 | 2 | A1 | B1C
C ^ | B1 | 2 | 3 | B1C
>The fisrt column itself is geting replace and the substitution is at wrong
>position.
**预期输出为**
A VAL|1|2|A1|
C VAL|2|2|B1|
D VAL|1|2|3|
答案 0 :(得分:2)
你可以试试这个awk:
awk 'BEGIN{OFS=FS="|"} $1 ~ /^A/{$(NF-1)="A1"} $1 ~ /^C/{$(NF-1)="B1"} 1' file.csv
A VAL|1|2|A1|
C VAL|2|2|B1|
D VAL|1|2|3|
答案 1 :(得分:0)
awk'BEGIN {OFS = FS =“|”} {if(substr($ 1,0,1)==“A”)sub($ 3,“A1”,$ 3);否则if(substr($ 1, 0,1)==“C”)sub($ 3,“B1”,$ 3);否则if(substr($ 1,0,1)==“D”)sub($ 3,“3”,$ 3);打印''inputtext.txt> outtext.txt
这很好用