1748019 Members
4467 Online
108757 Solutions
New Discussion юеВ

help with SED

 
iranzo
Frequent Advisor

help with SED

Hello,
here my script :
##
EXPORIG=toto
NEWEXP=bibi
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 ?
Thank's
Bonjour
8 REPLIES 8
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

Hi,
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.

Massimo

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

EXPORIG=toto
NEWEXP=bibi
echo sed \'s/$EXPORIG/$NEWEXP/g\' file > replace.sh
chmod +x replace.sh
./replace.sh

All paths lead to destiny
flavia
New Member

Re: help with SED

Hi,
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
1,$ s/$EXPORIG/$NEWEXP/g
w file
EOF


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

Hai
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')"
sed -e "s/$QUOTEDEXPORIG/$QUOTEDNEWEXP/g" file

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')"
sed -e "s/$QUOTEDEXPORIG/$QUOTEDNEWEXP/g" file


Caesar_3
Esteemed Contributor

Re: help with SED

Hello!

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

Caesar
James R. Ferguson
Acclaimed Contributor

Re: help with SED

Hi:

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

Regards!

...JRF...