0

I am writing a script to replace a string in a file in linux.

this is my code:

echo "Enter file name? "
read FILE1
echo "You have entered, $FILE1"

echo "String to be replaced "
read STR1
echo "You have entered, $STR1"

echo "Replace by "
read STR2
echo "You have entered, $STR2"

sed -i 's/$STR1/$STR2/g' /misc/home3/abc/$FILE1

echo "DONE !!"

The string is not getting replace in the file. what could be wrong with it??

Thanks :)

1
  • 2
    Variables aren't parsed in single quotes; use double quotes; ie. "s/$str1/$str2/g" Commented Jul 30, 2014 at 9:37

1 Answer 1

1

Do not use ' for the sed-string but " like this:

sed -i "s/$STR1/$STR2/g" /misc/home3/abc/$FILE1

Double quotes allow the shell to do variable expansion inside of the quoted text. Single tick ' prevents this.

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

Comments

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.