我有一个这样的文件:
PRODUCT: $name | YEAR: $year | TYPE: $type
PRODUCT2: $name2 | YEAR: $year | TYPE: $type2
PRODUCT3: $name3 | YEAR: $year | TYPE: $type
PRODUCT4: $name4 | YEAR: $year | TYPE: $type3
PRODUCT5: $name5 | YEAR: $year | TYPE: $type
PRODUCT6: $name6 | YEAR: $year | TYPE: $type
PRODUCT7: $name7 | YEAR: $year | TYPE: $type3
PRODUCT8: $name8 | YEAR: $year | TYPE: $type2
我想更改特定字段。
例如,我想更改产品5从$类型到$类型2但是我不想更换整条生产线,只有我想更改的字段!
这可能吗?
答案1
awk
可以帮助:
awk '{ if ($1 == "PRODUCT5:") $8 = "$type2"; print }' input
mv output input
或者sed
sed -i '/^PRODUCT5/s/$type$/$type2/' input