![將文件中的負值替換為零](https://rvso.com/image/1416699/%E5%B0%87%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E8%B2%A0%E5%80%BC%E6%9B%BF%E6%8F%9B%E7%82%BA%E9%9B%B6.png)
我想用零替換文件中的所有負值。我怎麼能在 awk 中做到這一點?
我嘗試過gsub
但gsub(-*, 0)
不起作用...有什麼想法嗎?
我的「代碼」是
awk '{gsub($(!/-/),"0",$2); print $1 "\t" $2} file.dat >file.dat
答案1
看起來您正在嘗試僅更新第二列。如果是這樣的話,這應該使:
awk '$2<0 {$2=0} 1' file > tmp_file && mv tmp_file file
測試
$ cat a
hello 2
hello 3
hello -1
hello -4
hello 0
$ awk '$2<0 {$2=0} 1' a
hello 2
hello 3
hello 0
hello 0
hello 0