bash - Programmatically feed parameters to sed -


say have bash script, first parses parameters/files. result, script generates following string:

args="-e 's/\\\$foo\\b/bar/gi' -e 's/\\\$baz\\b/qux/gi'" 

now want feed resulting string (-e 's/\$foo\b/bar/gi' -e 's/\$baz\b/qux/gi') sed in order perform search , replace on instance following file:

hello $foo, hello $baz 

if 1 uses sed -e 's/\$foo\b/bar/gi' -e 's/\$baz\b/qux/gi', returns:

hello bar, hello qux 

if 1 calls:

sed $args 

it gives error:

sed: -e expression #1, char 1: unknown command: `'' 

how can programmatically feed sequence of parameters sed?

avoid crazy escaping , declare args variable shell array:

args=(-e 's/\$foo\b/bar/gi' -e 's/\$baz\b/qux/gi') 

and use in sed as:

s='hello $foo, hello $baz' echo "$s" | sed "${args[@]}" hello bar, hello qux 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -