0

I want to replace all negative values in a file with zeros. How can I do this in awk?

I tried with gsub but gsub(-*, 0) doesn't work... Any ideas?

My "code" is

awk '{gsub($(!/-/),"0",$2); print $1 "\t" $2} file.dat >file.dat

1 Answer 1

3

It looks like you are trying to update just 2nd column. If that is the case, this should make:

awk '$2<0 {$2=0} 1' file > tmp_file && mv tmp_file file

Test

$ 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .