Reemplazar valores negativos en un archivo con ceros

Reemplazar valores negativos en un archivo con ceros

Quiero reemplazar todos los valores negativos de un archivo con ceros. ¿Cómo puedo hacer esto en awk?

Lo intenté gsubpero gsub(-*, 0)no funciona... ¿Alguna idea?

Mi "código" es

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

Respuesta1

Parece que estás intentando actualizar solo la segunda columna. Si ese es el caso, esto debería hacer:

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

Prueba

$ 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

información relacionada