Reputation: 11
I am trying to replace a string in a line with another string with quotation marks in a file, say, $FILE. I'm trying to use sed.
I want to replace m = uniform(0, 0, 1)
by m.LoadFile("run2_initial_$NF.ovf")
I am using this:
sed -i 's#m = uniform(0, 0, 1)#m.LoadFile("run2_initial_$NF.ovf")#g' $FILE
What I am getting is m = uniform(0, 0, 1)
replaced by m.LoadFile(run2_initial_$NF.ovf)
That is, sed is just ignoring the quotation marks in the replacement string.
Am I doing something stupid?
Please suggest.
Edit: The quotation mark is now working fine, when I try now. Though the $NF is not being replaced by a number :(
What I got in the new file is:
m.LoadFile("run2_initial_$NF.ovf")
whereas I wanted: m.LoadFile("run2_initial_3.ovf")
Upvotes: 1
Views: 140
Reputation: 8140
If you use double quotes, the variables inside the string will be replaced by their values, if you use single quotes, they won't.
What you need to do here is to replace the single quotes with double quotes, and escape the double quotes you already had:
$ echo "m = uniform(0, 0, 1)" | \
sed "s#m = uniform(0, 0, 1)#m.LoadFile(\"run2_initial_$NF.ovf\")#g"
m.LoadFile("run2_initial_3.ovf")
Upvotes: 1