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
  • 1
    Works fine for me. – John1024 Aug 5 '16 at 18:30
  • 1
    same here.. worked fine. – Rahul Aug 5 '16 at 18:31
  • Sorry, need to make an edit. – gkmohit Aug 5 '16 at 18:32
  • this one also worked for me – Rahul Aug 5 '16 at 18:37
  • 1
    After the edits which have significantly changed both the file and the command, it still works fine for me. – John1024 Aug 5 '16 at 18:46
3
sed 's/^\(John="\)Superman/\1'"$var1/g" <in file

...ought to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.