Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract and replace a token in shell script

In a source code file, I need to change all the instances like

foo_bar('param')

to become

booch['param']=$param

Here, param is a token and it can vary. It can be anything.

i.e., in a file,

all the

foo_bar('xxx')

have to become

booch['xxx']=$xxx

and,

all the

foo_bar('yyy')

have to become

booch['yyy']=$yyy

and so on. I'm trying to do it by writing a shell script and using sed. But couldnt figure out how to do it. Hope my query is understandable. Thanks

like image 266
Kartins Avatar asked Oct 25 '25 01:10

Kartins


1 Answers

sed "s/foo_bar('\([^']*\)')/booch['\1']=$\1/g" infile > outfile

Test

$ echo "foo_bar('xxx')" | sed "s/foo_bar('\(.*\)')/booch['\1']=$\1/"
booch['xxx']=$xxx
like image 196
SiegeX Avatar answered Oct 27 '25 14:10

SiegeX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!