Substitua valores negativos em um arquivo por zeros

Substitua valores negativos em um arquivo por zeros

Quero substituir todos os valores negativos em um arquivo por zeros. Como posso fazer isso no awk?

Tentei gsubmas gsub(-*, 0)não funcionou... Alguma ideia?

Meu "código" é

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

Responder1

Parece que você está tentando atualizar apenas a segunda coluna. Se for esse o caso, isso deve fazer:

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

Teste

$ 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

informação relacionada