Sorry for a really basic question. How to replace a particular column in a tab separated file with some string, say “xyz”?
e.g.
Input:
abc\t def\t \t xyz
pqr\t ert\t \t yut
Output:
abc\t def\t new_str\t xyz
pqr\t ert\t new_str\t yut
In short, the 3rd column in my file is empty and I want to replace it with a string.
I tied something like this:
awk '{$3="new_str"}1' test1.csv > test1_op.csv
but looks like it does not maintain the tabs. Also note that the file has 400 columns, so writing an awk command something like this:
awk -F, '{OFS=",";print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,...,$400}' in.csv > outfile.csv
I would like to avoid.
Thanks.
Since it’s tab-delimited, you are on the right track by splitting on a tab, rather than
awk‘s default of splitting on any whitespace (any combination of spaces, tabs, and possibly other more esoteric types). Once you’ve done that, you can assign directly to the column you want to change and just print $0 again.awkcan also process variable assignments that appear in its input file list, so this is a little cleaner-looking: