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

Sos
  • 1,783
  • 2
  • 20
  • 46

3 Answers3

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
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"
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    `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 Jun 11 '19 at 11:34
  • @paxdiablo Thanks. Since I almost always use `-F`, I never have to deal with the actual variable name. – Barmar Jun 11 '19 at 11:35
  • 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 – Sos Jun 11 '19 at 12:02
  • Nevermind, I found it in [here](https://stackoverflow.com/a/16491064/1379826) `awk -F"," -v quote="'" -v OFS="','" '{print quote $3,$4 quote}' $lookup_file` – Sos Jun 11 '19 at 12:10
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=,
vintnes
  • 2,014
  • 7
  • 16