4

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
6
  • 1
    Works fine for me.
    – John1024
    Commented Aug 5, 2016 at 18:30
  • 1
    same here.. worked fine.
    – Rahul
    Commented Aug 5, 2016 at 18:31
  • Sorry, need to make an edit.
    – gkmohit
    Commented Aug 5, 2016 at 18:32
  • this one also worked for me
    – Rahul
    Commented Aug 5, 2016 at 18:37
  • 1
    After the edits which have significantly changed both the file and the command, it still works fine for me.
    – John1024
    Commented Aug 5, 2016 at 18:46

1 Answer 1

3
sed 's/^\(John="\)Superman/\1'"$var1/g" <in file

...ought to work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .