1

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
3
  • i don't know where comes the field 11,14,17. Commented Dec 2, 2014 at 15:27
  • @AvinashRaj OP just want to do replacement on those 3 columns. Commented Dec 2, 2014 at 15:42
  • So is it important to retain the specific <space><tab><space>, <tab><space> and <space><tab> white space between fields as you show or can all field-separating white space be replaced with, say, a single tab character? The former is difficult, the latter is trivial. Commented Dec 3, 2014 at 1:08

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Don't need the =7 i don't think, BEGIN{a[11];a[14];a[17]} would suffice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.