I always kind of wondered this but I have a variable that I want to use in a search and replace. Basically I want to search a file for the string in my variable and replace it with something fixed but I'm unsure of the variable rule in sed. Here's generally what I have:
sed 's/$name/newname/g' file
Not sure if $name needs to be escaped or something. This always confuses me so if someone could help that'd be awesome! Many thanks!
ah ok cool thanks! But one more problem and i believe it has to do with something im substituting in. The variable $name contains something like... "<stuff>morestuff" without the quotes of course. I think the sed command is getting killed by the <> brackets though. Any fix for this? thanks!
I tried with and without the double quotes. I tried with single quotes around the sed and that didnt seem to do anything. Not sure if it mattered but the line before this sed command I also set the name variable to something so this isn't a variable being passed in
no luck as in it didn't do anything. no error and didn't produce the right result. I'll paste the script so maybe u can see if other things are causing issues
#!/bin/sh
name=$(grep ENGINE radar)
echo ${name}
sed "s#${name}#ENGINE#g" template> output
The word ENGINE is what I want to replace in file template with whatever gets put into the $name variable. the result right now is that output is the same file as template