Awk or Sed to Change Column Value in a File

change a particular column value using awk / sed

Set the OFS (output field seperator) to be the same as your FS (Field Seperator which is set with your -F flag) in the BEGIN clause of your awk script otherwise it's set to the default of " "

awk -F \| 'BEGIN{OFS=FS}{$11=1; print}' input_file.txt 

awk or sed to change column value in a file

awk '{$5=sprintf( "%.2g", $5)} 1' OFS=, FS=, input

This will round and print .47 instead of .46 on the first line, but perhaps that is desirable.

changing value of first column in a file using awk

This might work for you (GNU sed):

sed '/^negative,/!s/^[^,]*/positive/' file

If the first word is not negative change it to positive.

How to replace a value to another value in a specific column on a gzipped file using awk?

You could check only for X in the first column and check if the row number is greater than 1.

Then you can replace X at the start of the string using ^X with 23.

awk 'NR > 1 && $1=="X" {sub(/^X/,"23")}1' > out.txt

replace strings in column with matching value from another file using awk

You didn't reference the associative array in the right way. Please change:

...{if ($3 in a){$3=a[1]}; print $0...

into:

...{if ($3 in a){$3=a[$3]}; print $0

The keys of your array a are AAA,AAB... instead of 1,2,3....

Linux - Replace column value

This might work for you (GNU sed):

sed 's/^\(\([^|]*|~|\)\{16\}[^|]*\)\.000000\([^|]*|~|\)/\1\3/' file

Match 16 fields and the first part of the 17th field, followed by .000000 and the last part of the 17th field followed by |~| and replace it by 16 fields and the first part of the 17th field followed by the last part of the 17th field and|~|.



Related Topics



Leave a reply



Submit