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.

6
  • 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
    Commented Apr 23, 2020 at 8:00
  • Is this what you are after? $ echo 'text1,text2,add(foo,bar),text3' | sed 's/add(\([^(),]\+\),\([^,()]\+\))/\1 \2 c/'
    – kvantour
    Commented Apr 23, 2020 at 8:08
  • Thank you!!! That's exactly what I was after
    – help_me
    Commented Apr 23, 2020 at 8:13
  • @kvantour Feel free to add an answer. Commented Apr 23, 2020 at 8:29
  • @WiktorStribiżew There is no need to make this an answer. There are enough questions out there regarding back referencing.
    – kvantour
    Commented Apr 23, 2020 at 8:54

0

Browse other questions tagged or ask your own question.