sed converting / to \/


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users sed converting / to \/
# 1  
Old 12-12-2006
sed converting / to \/

Hi all,
I am using sed for converting a string of type /abc/def/gh by \/abc\/def\/gh

[trainee@LINUX ~]$ edu="/home/abc/dec"

When I echo that variable and pass it to sed ,it works fine...
[trainee@LINUX ~]$ echo $edu|sed 's/\//\\\//g'
\/home\/abc\/dec

But When I try to store in a variable , it shows the following error Smilie
[trainee@LINUX ~]$ ued=`echo $edu|sed 's/\//\\\//g'`
sed: -e expression #1, char 9: unknown option to `s'

HELP BLEAZ

Last edited by sakthi.abdullah; 12-12-2006 at 09:23 AM..
# 2  
Old 12-12-2006
Code:
echo "a/b/d" | sed 's#/#\\/#g'

When you are using sed to work with "/" characters you have to use another delimiter - in this case I picked "#"
# 3  
Old 12-13-2006
Can you explain this further because
Usually it's of the form s/old/new/g but sed 's#/#\\/#g' contains only s/.../g
# 4  
Old 12-13-2006
Quote:
Originally Posted by jim mcnamara
Code:
echo "a/b/d" | sed 's#/#\\/#g'

When you are using sed to work with "/" characters you have to use another delimiter - in this case I picked "#"
Again, echo "a/b/d" | sed 's#/#\\/#g' ll work fine.... But What I need is something like this ..how can I store that value on to a variable
like abc=`echo "a/b/d" | sed 's#/#\\/#g'`
# 5  
Old 12-13-2006
Quote:
Originally Posted by sakthi.abdullah
abc=`echo "a/b/d" | sed 's#/#\\/#g'`
Turn the backticks into $(...). See this.

Code:
[~]$ abc=`echo "a/b/d" | sed 's#/#\\/#g'`
[~]$ echo $abc
a/b/d
[~]$ abc=$(echo "a/b/d" | sed 's#/#\\/#g')
[~]$ echo $abc
a\/b\/d
[~]$

Hmm.. I am a little surprised with the difference in behaviour of `...` and $(...) Smilie

Edit
Ah ! Here's how it goes with the backticks.
Code:
[/tmp]$ abc=`echo "a/b/d" | sed 's#/#\\\\/#g'`
[/tmp]$ echo $abc
a\/b\/d

And here's why. From man sh
Code:
       When  the  old-style  backquote  form of substitution is used, backslash
       retains its literal meaning except when followed by $,  ?,  or  \.   The
       first  backquote not preceded by a backslash terminates the command sub-
       stitution.  When using the $(command) form, all characters  between  the
       parentheses make up the command; none are treated specially.

/Edit

Last edited by vino; 12-13-2006 at 01:22 AM..
# 6  
Old 12-13-2006
Backtick version:
abc=`echo "a/b/d" | sed 's#/#\\\\/#g'`

but I strongly prefer $(...) to `...` for many reasons.
# 7  
Old 12-14-2006
Quote:
Originally Posted by Perderabo
Backtick version:
abc=`echo "a/b/d" | sed 's#/#\\\\/#g'`

but I strongly prefer $(...) to `...` for many reasons.
Can you please mention those ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in converting

I have Excel file with the below three columns, i need your expertise in converting this to .csv file delimiter "|" Excel - Serial Number Serial Name Serial Brand 111 test sample 123 test2 sample1 134 ... (9 Replies)
Discussion started by: kiran_hp
9 Replies

2. Shell Programming and Scripting

Converting txt file into CSV using awk or sed

Hello folks I have a txt file of information about journal articles from different fields. I need to convert this information into a format that is easier for computers to manipulate for some research that I'm doing on how articles are cited. The file has some header information and then details... (8 Replies)
Discussion started by: ksk
8 Replies

3. Shell Programming and Scripting

Converting a line into a list using awk or sed

Hello, I am trying to convert a line into a list using awk or sed. Line: 345 897 567 098 123 output: 345 897 567 098 123 thanks (7 Replies)
Discussion started by: smarones
7 Replies

4. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

5. UNIX for Advanced & Expert Users

Need help converting ctlU (^U) to a \12...

I have a file that ends with a ctlU: > cat -v test.file blah,blah,blah,GEAEA*1*xx0000111xxx^UIEA*xxx0^U would like to replace the ctlU (^U) with a "\12"...due to printers or something. I believe I might be able to utilize the tr command, if I could only identify the correct... (4 Replies)
Discussion started by: mr_manny
4 Replies

6. SCO

library converting

Hi everybody Is there any sco unix command to convert .so library to .a (under sco unix openserver.5.0.6) tnx (2 Replies)
Discussion started by: javad1_maroofi
2 Replies

7. Shell Programming and Scripting

Converting \0 to a \n

Hi - I have seen some similar posts but I am a bit stumped here below is the first line of a 'od -c filename' command. I want to change the \0 to \n 0000000 l s \0 c d - \0 c d . . \0 l s I have tried a sed construct in a script......... sed... (2 Replies)
Discussion started by: ajcannon
2 Replies

8. SCO

Converting

I use Sco_Sv 3.2v5.0.5 with parellel conection using dump terminals and i want to convert them to desktop pc. Anybody knows what hardware and other thing that would be involved? (3 Replies)
Discussion started by: seeyou
3 Replies

9. Shell Programming and Scripting

Converting to Uppercase

I want to convert string into uppercase string. How can i do that ? Ex: Enter the user name: read name show=upper(name) echo $show --- This output should be the uppercase output. Thanks (3 Replies)
Discussion started by: dreams5617
3 Replies

10. UNIX for Dummies Questions & Answers

converting kb to mb

When I create filesystems in AIX i often get confused(using smit) When you specify size in aix, it is asked like this SIZE of file system (in 512-byte blocks) I never seem to grasp this, what is the equation to get say 500mb? Or is there a program anyone knows of that does this, like a... (1 Reply)
Discussion started by: csaunders
1 Replies
Login or Register to Ask a Question