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

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
Honored Contributor
Ian Dennison_1
Posts: 1,139
Registered: ‎11-08-2001
Message 2 of 9 (252 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
Massimo Bianchi
Posts: 2,472
Registered: ‎01-18-1999
Message 3 of 9 (252 Views)

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

Esteemed Contributor
John Meissner
Posts: 887
Registered: ‎07-01-2002
Message 4 of 9 (252 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

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

All paths lead to destiny
Occasional Visitor
flavia
Posts: 1
Registered: ‎05-20-2003
Message 5 of 9 (252 Views)

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

Re: help with SED

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

With this:

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

Hai
Honored Contributor
Mike Stroyan
Posts: 888
Registered: ‎11-22-1996
Message 7 of 9 (252 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')"
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


Esteemed Contributor
Caesar_3
Posts: 952
Registered: ‎06-05-2003
Message 8 of 9 (252 Views)

Re: help with SED

Hello!

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

Caesar
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 9 (252 Views)

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