Fancy awk output without using printf

619 views Asked by At

This is just for curiosity.

I have a tab delimited file like this

jklh    banana  hk
hkl klh jklh
h   hk  banana
h   hk  kljh
asdf    banana  lk
sdfa    jklæ    jklæ
banana  sdf jklæ

By doing this I replace 'banana' in the first column and the output stays otherwise the same:

awk '{gsub(/banana/,0,$1)}; {printf "%s\t%s\t%s\n", $1, $2, $3}' file > outfile

This on the other hand replaces tabs with spaces in the line I replaced the word banana with 0:

awk '{gsub(/banana/,0,$1)}; {print}' file > outfile

How can I use OFS or something similar to print out the replaced line without replacing tabs. I've been playing around alot without progress. Remember: No printf

1

There are 1 answers

2
Michael J. Barber On BEST ANSWER

Set OFS to a tab:

awk -v OFS="\t" '{gsub(/banana/,0,$1)}; {print}'