如果在 input.txt 中,我想仅将 ID 等于或大于 4000 的行的最后一列的元素替换为字符串“P”,我该怎么办?
即我只想将第一行(ID = 4304)和第三行(ID = 4000)的最后一个元素(“N”和“C”)替换为“P”。
输入.txt
4304,N,-9.700,-7.680,58.330,-2.3,N
2940,S,-10.440,-3.450,54.270,2.2,S
4000,C,-13.655,-13.730,59.405,-1.5,C
2931,C,-9.910,-2.420,57.610,0.2,C
输出.txt
4304,N,-9.700,-7.680,58.330,-2.3,P
2940,S,-10.440,-3.450,54.270,2.2,S
4000,C,-13.655,-13.730,59.405,-1.5,P
2931,C,-9.910,-2.420,57.610,0.2,C
我该怎么办?谢谢!
答案1
类似的东西可以完成这项工作:
awk -F, '$1=="4304" || $1=="4000" {$NF="P"}1 ' OFS=, input.txt >output.txt