Skip to main content
edited tags
Link
don_crissti
  • 85.6k
  • 31
  • 234
  • 262
Added more details about the file.
Source Link
gkmohit
  • 3.4k
  • 8
  • 29
  • 31

So right now I have a variable

var1='batman'

I also have a file wrong.txt that reads as follows

SupermanJohn="Superman lives in Gotham City."
Superman'sJohn="Superman's parents were killed in Crime Alley."
SupermanJames="Superman hasdoes not have a bat-mobile."

Now I want to change the file's contents using sed. Obviously, I need to replace Superman with batman. I tried doing it with sed, but there were no changes made to the file.

Here is what I did

sed -i -e "s/SupermanJohn=\"Superman/$var1John=\"$var1/g" wrong.txt

So right now I have a variable

var1='batman'

I also have a file wrong.txt that reads as follows

Superman lives in Gotham City.
Superman's parents were killed in Crime Alley.
Superman has bat-mobile.

Now I want to change the file's contents using sed. Obviously, I need to replace Superman with batman. I tried doing it with sed, but there were no changes made to the file.

Here is what I did

sed -i -e "s/Superman/$var1/g" wrong.txt

So right now I have a variable

var1='batman'

I also have a file wrong.txt that reads as follows

John="Superman lives in Gotham City."
John="Superman's parents were killed in Crime Alley."
James="Superman does not have a bat-mobile."

Now I want to change the file's contents using sed. Obviously, I need to replace Superman with batman. I tried doing it with sed, but there were no changes made to the file.

Here is what I did

sed -i -e "s/John=\"Superman/John=\"$var1/g" wrong.txt
Source Link
gkmohit
  • 3.4k
  • 8
  • 29
  • 31

Use sed to replace a part of a line with a variable

So right now I have a variable

var1='batman'

I also have a file wrong.txt that reads as follows

Superman lives in Gotham City.
Superman's parents were killed in Crime Alley.
Superman has bat-mobile.

Now I want to change the file's contents using sed. Obviously, I need to replace Superman with batman. I tried doing it with sed, but there were no changes made to the file.

Here is what I did

sed -i -e "s/Superman/$var1/g" wrong.txt