cancel
Showing results for 
Search instead for 
Did you mean: 

Sed Command

Sivasubramanian S
Occasional Visitor

Sed Command

What does the below command retuens?

temp=`echo "$1" | sed "s/'/\\\\\'/g" | sed 's/ /\\\\ /g'`

Please explain with example. Thanks a lot.

Siva
6 REPLIES
Steven Schweda
Honored Contributor

Re: Sed Command

> What does the below command retuens?

"Life is like a sewer. What you get out of
it depends on what you put into it."

dyi # echo "a'b c'd" | sed "s/'/\\\\\'/g" | sed 's/ /\\\\ /g'
a\'b\\ c\'d

Note, however, that:

dyi # echo "a'b c'd" | sed "s/'/\\\'/g" | sed 's/ /\\\\ /g'
a\'b\\ c\'d

> Please explain with example.

Please supply some context. Is there some
actual problem which you are trying to solve?
Dennis Handly
Acclaimed Contributor

Re: Sed Command

It seems for every blank it adds a "\" before them. And for every "'" it adds two "\\" before them.

And when this pipeline is wrapped in $(), that double backslash is converted into just one.
$ script.sh "xx'yy zz"
xx\'yy\ zz
xx\'yy\\ zz # with just echo

Hmm, it appears sed gets different number of backslashes depending on whether $() or ``.
But it seems the output is the same:
$():
argv[0]: "sed"
argv[1]: "-e"
argv[2]: "s/'/\\\'/g"
argv[3]: "-e"
argv[4]: "s/ /\\\\ /g"

``:
argv[0]: "sed"
argv[1]: "-e"
argv[2]: "s/'/\\'/g"
argv[3]: "-e"
argv[4]: "s/ /\\ /g"

Neither:
argv[0]: "sed"
argv[1]: "-e"
argv[2]: "s/'/\\\'/g"
argv[3]: "-e"
argv[4]: "s/ /\\\\ /g"

>Please explain with example

So basically this (how many backslashes you need) can't be explained very well. :-(
Sivasubramanian S
Occasional Visitor

Re: Sed Command

Thanks Dennis.

P.S Since this was part of a generic script, the purpose of which could not be understood, I had to frame such a generic question.

Siva
OldSchool
Honored Contributor

Re: Sed Command

the snippet posted *might* be of use when the contents of "temp" is echoed to a file, and the contents of the file is subsequently read into a shell variable. In that case, the spaces and single-qoutes are retained (although double-qoutes are a different matter)
Sivasubramanian S
Occasional Visitor

Re: Sed Command

Here is the output of the command.

/home/test>cat file1
123
'123'
' 123 '
' 123'
'123
' 123

/home/test>sed "s/'/\\\\\'/g" file1

123
\'123\'
\' 123 \'
\' 123\'
\'123
\' 123

/home/test>sed "s/'/\\\\\'/g" file1|sed 's/ /\\\\ /g'

123
\'123\'
\'\\ 123\\ \'
\'\\ 123\'
\'123
\'\\ 123


Not sure of the choice of the number of back slashes.
OldSchool
Honored Contributor

Re: Sed Command

This appears to be an example of what it *could* be used for:


fsm.sh
====================================================
#!/bin/bash
temp=`echo "$1" | sed "s/'/\\\\\'/g" | sed 's/ /\\\\ /g'`
echo $temp


fsm1.sh
====================================================
#!/bin/bash
read temp2
echo $temp2



sample use:
===================================================

A:

./fsm.sh "12 db ' ' 456" > oo.out
./fsm1.sh < oo.out
12 db ' ' 456

B:
./fsm.sh "12 db ' ' 456" | ./fsm1.sh
12 db ' ' 456

As noted previously, its used to maintain qoutes and spacing....