Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Showing results for 
Search instead for 
Did you mean: 

help with SED

Frequent Advisor

help with SED

here my script :
sed -e 's|"$EXPORIG"|"$NEWEXP"|g' file
the syntax in sed command is not correct
i want to replace "$EXPORIG" with "$NEWEXP"
in "file"
how to insert a variable in sed command ?
Ian Dennison_1
Honored Contributor

Re: help with SED

I am assuming that you do not want to retain the " (double quote) symbols?

sed "s/$EXPORIG/$NEWEXP/g" file

tested with a pipe command for the file, but still tested!

Share and Enjoy! Ian
Building a dumber user
Massimo Bianchi
Honored Contributor

Re: help with SED

Ian gave you an answer.

I think that your command was not working because the | was mis-interpreted by the shell.

Simply choosing a different separator will do the trick.


John Meissner
Esteemed Contributor

Re: help with SED

I have noticed a problem with putting variables into sed statements before as well. if Ian's statement above doesn't work you could try this as a last result

echo sed \'s/$EXPORIG/$NEWEXP/g\' file >
chmod +x

All paths lead to destiny
Occasional Visitor

Re: help with SED

whith sed you have need to redirect the output in a new file to save the change.
A friend tell me to try this for change and save in the same file,and its works.

Maybe is a good idea to make a back_up of file befour before!!!!

ed - file << EOF
w file

Sorry for my english ;)
Hai Nguyen_1
Honored Contributor

Re: help with SED

Replace this:
sed -e 's|"$EXPORIG"|"$NEWEXP"|g' file

With this:

eval sed 's/$EXPORIG/$NEWEXP/g' file

Mike Stroyan
Honored Contributor

Re: help with SED

The issue here is quoting of special characters such as | and " in combination with not quoting shell variables such as $EXPORIG.

Ian's solution will handle most cases where you did not really want the "'s to appear in the pattern you replace-
sed "s/$EXPORIG/$NEWEXP/g" file
The double quotes will quote the special characters but allow the variables to be expanded.

His solution will have trouble if there are any / symbols in the variables. Using | for the sed substitution will handle / characters in the pattern, but won't handle | characters in the pattern. To handle all strings in the patterns you will need to actively quote the character that you are using to delimit the patterns. Here is an example that uses / characters as the delimiter. The first two sed commands add quoting backslash characters as needed.
QUOTEDEXPORIG="$(print $EXPORIG | sed -e 's|/|\\/|g')"
QUOTEDNEWEXP="$(print $NEWEXP | sed -e 's|/|\\/|g')"

You might want to quote other wildcard characters as well. Here is an example that quotes every special character. It can handle characters like . and * in the patterns.
QUOTEDEXPORIG="$(print $EXPORIG | sed -e 's|[/.[\*^]|\\&|g')"
QUOTEDNEWEXP="$(print $NEWEXP | sed -e 's|[/.[\*^]|\\&|g')"

Esteemed Contributor

Re: help with SED


In sed you use "/" and not "|"
do this:
sed "s/${EXPORIG}/${NEWEXP}/g"

James R. Ferguson
Acclaimed Contributor

Re: help with SED


As a point of clarification (and I quote from the 'sed' man pages since it says it so well!)"

"An address is either a decimal number that counts input lines cumulatively across files, a $ which addresses the last line of input,
or a context address; that is, a /regular expression/... In a context address, the construction \?regular expression?, where ? is any character, is identical to /regular expression/."

For substitutions, "s/regular expression/replacement/flags: Substitute replacement string for instances of regular xpression in the pattern space. Any character can be used instead of /."