0

I have a comma-delimited file to which I want to append a string in specific columns. I am trying to do something like this, but couldn't do it until now.

re1,1,a1e,a2e,AGT
re2,2,a1w,a2w,AGT
re3,3,a1t,a2t,ACGTCA
re12,4,b1e,b2e,ACGTACT

And I want to append 'some_string' to columns 3 and 4:

re1,1,some_stringa1e,some_stringa2e,AGT
re2,2,some_stringa1w,some_stringa2w,AGT
re3,3,some_stringa1t,some_stringa2t,ACGTCA
re12,4,some_stringb1e,some_stringb2e,ACGTACT

I was trying something similar to the suggestion solution, but to no avail:

awk -v OFS=$'\,' '{ $3="some_string" $3; print}' $lookup_file

Also, I would like my string to be added to both columns. How would you do this with awk or bash?

Thanks a lot in advance

2
  • Why tagged bash and not awk? Commented Jun 11, 2019 at 18:32
  • @ctac_ I added the awk tag now. My initial idea was not to go specifically for awk, but for either awk or bash. Commented Jun 12, 2019 at 7:36

3 Answers 3

2

You can do that with (almost) what you have:

pax> echo 're1,1,a1e,a2e,AGT
re2,2,a1w,a2w,AGT
re3,3,a1t,a2t,ACGTCA
re12,4,b1e,b2e,ACGTACT' | awk 'BEGIN{FS=OFS=","}{$3 = "pre3:"$3; $4 = "pre4:"$4; print}'

re1,1,pre3:a1e,pre4:a2e,AGT
re2,2,pre3:a1w,pre4:a2w,AGT
re3,3,pre3:a1t,pre4:a2t,ACGTCA
re12,4,pre3:b1e,pre4:b2e,ACGTACT

The begin block sets the input and output field separators, the two assignments massage fields 3 and 4, and the print outputs the modified line.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to set FS to comma, not just OFS. There's a shortcut for setting FS, it's the -F option.

awk -F, -v OFS=',' '{ $3="some_string" $3; $4 = "some_string" $4; print}' "$lookup_file"

4 Comments

IFS is a bash thing, not an awk thing. In awk, the input field separator is just FS. That doesn't mean your solution is wrong, just that you're using the wrong terminology.
@paxdiablo Thanks. Since I almost always use -F, I never have to deal with the actual variable name.
Thanks a lot @paxdiablo and @Barmar. I'm wondering, what if the string to add has single quotations? I've tried with "\'" but it doesnt work
Nevermind, I found it in here awk -F"," -v quote="'" -v OFS="','" '{print quote $3,$4 quote}' $lookup_file
1

awk's default action is to concatenate, so you can simply place strings next to each other and they'll be treated as one. 1 means true, so with no {action} it will assume "print". You can use Bash's Brace Expansion to assign multiple variables after the script.

awk '{$3 = "three" $3; $4 = "four" $4} 1' {O,}FS=,

Comments

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.