algoProg algoProg - 1 year ago 109
Bash Question

Bash script sed

I am trying to use

in bash script as follows:

for i in `seq 1 10`;
OLD="-option_something something/string1_${j}.txt"
NEW="-option_somehting something/string1_${i}.txt"
sed -e "s/$OLD/$NEW/g" file_to_edit.txt
# sed -e "s/$OLD/$NEW/g" file_to_edit.txt > file_to_edit.txt.tmp && mv file_to_edit.txt.tmp file_to_edit.txt

But I keep getting following error:

sed: -e expression #1, char 71: unknown option to

I tried the commented line as well, but it does not work too.

It works fine on command line. I do not know what is the problem in script.

Any suggestions? Thanks.

Answer Source

You have a / in the value of OLD and NEW, which is the same character you're using as the delimiter in your sed expression. So the final expression ends up looking like:

sed -e "s/-option_something something/string1_${j}.txt/-option_somehting something/string1_${i}.txt/g"

Do you see all the / in there? Consider instead:

sed -e "s|$OLD|$NEW|g" file_to_edit.txt

You can use any character as the delimiter for sed's s command.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download