0

I'm struggling with the following:

Current File:

text1,text2,add(a,b),text3

Target File:

text1,text2,a b c,text3

where a and b vary.

Example:

text1,text2,add(11,12),text3
text1,text2,add(41,42),text3

would become:

text1,text2,11 12 c,text3
text1,text2,41 42 c,text3

I would like to do something like:

sed -i "s/add(*,*)/ a b c/g"

But I'm pretty sure the use of wildcards is completely wrong.

New to bash scripting, any advice is appreciated. Thanks.

Edit: To clarify, I want to save a and b into variables so that I can print them again.

help_me
  • 1
  • 2
  • Sorry but your question is unclear to me. What do you mean with `$a` and `$b`? Do you want to replace the content of `add(a,b)` with `$a $b c` where `$a` is the content of a shell variable named `a` (dito `b`)? – kvantour Apr 23 '20 at 08:00
  • Is this what you are after? `$ echo 'text1,text2,add(foo,bar),text3' | sed 's/add(\([^(),]\+\),\([^,()]\+\))/\1 \2 c/'` – kvantour Apr 23 '20 at 08:08
  • Thank you!!! That's exactly what I was after – help_me Apr 23 '20 at 08:13
  • @kvantour Feel free to add an answer. – Wiktor Stribiżew Apr 23 '20 at 08:29
  • @WiktorStribiżew There is no need to make this an answer. There are enough questions out there regarding back referencing. – kvantour Apr 23 '20 at 08:54
  • @kvantour If you know it is a duplicate, please always vote for it, otherwise, it is not clear if you want to answer later once OP confirms your solution is working for them. I always vote first, then write a comment. My database of awk/sed solutions is not yet as big as the regex one :(. – Wiktor Stribiżew Apr 23 '20 at 08:59

0 Answers0