Here's a similar question but I ran into a problem that was not addressed in the question.
How to replace the content of a specific column with awk?
I want to do something similar, however my file is tab delimited instead of space delimited. I want to change field 3. Lets say my file looks like this:
NAME\tNUMBER\tID\tSEQ\t...\t...\t...
Lets say I want to change field 3 to AD
awk '{$3 = "AD"; print}' infile > outfile
my output is
NAME\tNUMBER\tAD SEQ ... ... ... ... ... ...
my tabs turn into spaces after the replaced
I'm sure I can just tr
the output file but I want to know what causes awk
to change the field delimiter.
expected output:
NAME\tNUMBER\tAD\tSEQ\t...\t...\t...\t
Best Answer
You need to set
O
utputF
ieldS
eparator, to tab\t
:One way to do it is with
-v
option:another possibility inside
awk
, say in the BEGIN block:If you don't set
OFS
, thenawk
by default uses single space as a field separator.