1. sed and '/' to '\/' conversion.
I would love for someone to explain the following problem I am having with
sed. I am using the Bourne Shell under HP-UX 9.01.
The task : To use sed to manipulate a variable from having /'s in the string
to having \/'s in the string.
Example : Converting a/b/c to a\/b\/c
Notes : I am far from a sed guru, but I can usually get it to do what I
want (as long as it's simple enough).
Okay, this is what I go about doing.
First I try to do it on the command line :
% echo "a/b/c" | sed -e "s/\//\\\\\//g"
Then I put it into a script.
#!/bin/sh
VAR="a/b/c"
NEWVAR=`echo "$VAR" | sed -e "s/\//\\\\\//g"`
and try to run it.
sed: command garbled: s/\//\\//g
but if I try :
#!/bin/sh
convert ()
{
echo "$1" | sed -e "s/\//\\\\\//g"
}
VAR="a/b/c"
NEWVAR=`convert $VAR`
echo "NEWVAR is : $NEWVAR"
and try to run it.
NEWVAR is : a\/b\/c
Okay, I don't get it! What is the difference? Why does one work and the
other not work?
Well, not one to let sleeping dogs lie, I keep pounding away at it and put
together the following :
#!/bin/sh
VAR="a/b/c"
NEWVAR=`echo "$VAR" | sed -e "s/\//\\\\\\\\//g"`
echo "NEWVAR is : $NEWVAR"
NEWVAR=`echo "$VAR" | sed -e "s/\//\\\\\\\\\//g"`
echo "NEWVAR is : $NEWVAR"
NEWVAR=`echo "$VAR" | sed -e "s/\//\\\\\\\\\\//g"`
echo "NEWVAR is : $NEWVAR"
NEWVAR=`echo "$VAR" | sed -e "s/\//\\\\\\\\\\\//g"`
echo "NEWVAR is : $NEWVAR"
NEWVAR=`echo "$VAR" | sed -e "s/\//\\\\\\\\\\\\\//g"`
echo "NEWVAR is : $NEWVAR"
which when run results in :
sed: command garbled: s/\//\\//g
NEWVAR is :
NEWVAR is : a\/b\/c
NEWVAR is : a\/b\/c
NEWVAR is : a\/b\/c
sed: command garbled: s/\//\\\\//g
NEWVAR is :
Okay, now I'm really confused, why do the THREE in the middle work?
Any explanations much appreciated.
/Jim
--
------------------------------------------------------------------------------
Atmospheric Environment Service
2. Ensoniq Soundscape driver broken in 2.0.3x ?
3. Does sed '/'$var'/d' textfile work if $var has / in it???
4. Simple Method for HTTP GET Request
5. alpha sort: 'sed'/'awk'?
6. how to change shell
7. Can 'require' override 'allow'/'deny' (Apache)
8. File execution problem
9. Why does 'ls' give '/' as the output?
10. replacing '/' with '\\'
11. Why does 'ls' give '/' as the output?
12. How to do 'tunefs' on '/'
13. 'mail'/'sendmail' with higher priority??