0

I am trying to use sed in bash script as follows:

#!/bin/bash
for i in `seq 1 10`;
do
    j=$(($i-1))
    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 
done

But I keep getting following error:

sed: -e expression #1, char 71: unknown option tos'`

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Additionally I has to use -i instead of -e as my file was not getting edited and saved but just displayed in console and original file intact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.