0

I have a file and from this file I am trying to find a word and replace it with another word using Bash. I am using sed to do this and please note that the word that I am looking for is an output from a command. So I am trying to find a word, which is the output of a command, and replace it with another word and override the previous word.

This is my code:

File=file.txt
File2=file2.txt
min=$(cat $File2 | grep word);  
sed -i 's/$min/max/g' $File

It's not producing any error, but I am unable to find the word in order to replace it. When I manually type the word rather than using the variable "$min" it works just fine. So when I do this, it works:

sed -i 's/min/max/g' $File

but when I do this, it doesn't:

 sed -i 's/$min/max/g' $File

I am thinking maybe sed doesn't accept variables as a search string. Any idea how I can achieve this?

thank you.

2
  • 1
    Variables aren't expanded within single quotes. Commented Mar 9, 2017 at 12:32
  • Thanks for. I changed it to double quotes and it worked. Commented Mar 15, 2017 at 10:54

1 Answer 1

2

Use double quotes for the sed expression, this should work:

sed -i "s/$min/max/g" $File
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.