Hey!
I'm trying to use sed to search through a text, and replace
a variable containing spaces with 'bar'.
But i don't want to replace on lines containing 'foo'.
The reason i want to use a variable is that the sed-command is in a for-loop, so it get's a different variable each loop.
It works when i want to replace on lines
with foo:
Code:
$ echo ${test}
test1 test2
$ echo ${test// /\\ }
test1\ test2
$ cat file
test1
test2
replace this test1 test2
don't replace when foo test1 test2
$ sed "/foo/s/${test// /\\ }/bar/g" file
test1
test2
replace this test1 test2
don't replace when foo bar
But when i want to negate lines, bash freaks out.
Code:
$ sed "/foo/!s/${test// /\\ }/bar/g" file
bash: !s/${test//: event not found
Also, this works of course:
Code:
$ sed '/foo/!s/test1\ test2/bar/g' file
test1
test2
replace this bar
don't replace when foo test1 test2
But i have to use double quotes for sed to understand my variable.
Otherwise i get:
Code:
$ sed '/foo/s/${test// /\\ }/bar/g' file
sed: -e expression #1, char 17: unknown option to `s'
So, can i negate the regular expression
foo without using !
Or can i use sed-branching to test if the regexp foo is not on a line?
Or does anyone have a good hint on how to achieve this? Thanks!