This sed s
command was created for:
shopt -s extglob ORIG="foo(1,2)" REP="if (a > 1) { foo(1,2) } else { bar(1,2) }" REP="${REP//+( )/\\n}" sed "s/$ORIG/$REP/g" inputfile > outputfile
Please note that the lines REP="${REP//\+( )/\\n}"
necessary only if you want to define REP
in the formatted format that I did on the second line. It might be easier if you just used \n
and \t
in REP
to start.
Edit in response to an OP question
To change the source file without creating a new file, use the sed --in-place
flag, for example:
sed --in-place "s/$ORIG/$REP/g" inputfile
Please be careful with the --in-place
flag. Make backups before starting, because all changes will be permanent.
source share