Maris
Maris

Reputation: 23

awk replace in multiple columns specific character with one command

Input (a.txt)

aa <tab> c-c-c<tab>k-k-k<tab>
ll <tab> j-j-j <tab>v-v-v<tab>

Needed output (b.txt)

aa <tab> c.c.c<tab> k.k.k<tab>
ll <tab> j.j.j <tab>v.v.v<tab>

Please correct me: (this does not work, works only if I ask to replace in $11 column)

awk -F "\t" -v OFS="\t" '{gsub("-",".",$11,&14,&17); print;}' /home/a.txt > /home/b.txt

Upvotes: 1

Views: 2416

Answers (2)

Kent
Kent

Reputation: 195289

it seems that you don't want to do the replacement on whole line, just 3 columns, then just do this:

awk -F "\t" -v OFS="\t" 'BEGIN{a[11]=a[14]=a[17]=7}
                      {for(x in a)gsub("-",".",$x)}7' /home/a.txt > /home/b.txt

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174874

You don't need to specify the field number in gsub function if you want to do a global substitution on all the fields.

awk '{gsub(/-/,".")}1' /home/a.txt > /home/b.txt

Upvotes: 2

Related Questions