Re: help with SED (289 Views)
Frequent Advisor
Posts: 85
Registered: ‎10-24-2001
Message 1 of 9 (289 Views)

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 ?
Honored Contributor
Posts: 1,139
Registered: ‎11-08-2001
Message 2 of 9 (289 Views)

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
Honored Contributor
Posts: 2,472
Registered: ‎01-18-1999
Message 3 of 9 (289 Views)

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.


Esteemed Contributor
Posts: 887
Registered: ‎07-01-2002
Message 4 of 9 (289 Views)

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
Posts: 1
Registered: ‎05-20-2003
Message 5 of 9 (289 Views)

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 ;)
Honored Contributor
Posts: 691
Registered: ‎05-28-2001
Message 6 of 9 (289 Views)

Re: help with SED

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

With this:

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

Honored Contributor
Posts: 888
Registered: ‎11-22-1996
Message 7 of 9 (289 Views)

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
Posts: 952
Registered: ‎06-05-2003
Message 8 of 9 (289 Views)

Re: help with SED


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

Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 9 (289 Views)

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 /."


The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.