AWE
AWE

Reputation: 4135

Fancy awk output without using printf

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

Upvotes: 1

Views: 623

Answers (1)

Michael J. Barber
Michael J. Barber

Reputation: 25052

Set OFS to a tab:

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

Upvotes: 2

Related Questions