The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
converting hex to dec aismann Shell Programming and Scripting 4 02-26-2008 01:51 AM
Converting \0 to a \n ajcannon Shell Programming and Scripting 2 09-28-2007 08:30 AM
Converting seeyou SCO 3 01-19-2006 10:22 AM
converting .txt laila63 UNIX for Dummies Questions & Answers 4 06-30-2004 01:56 PM
converting kb to mb csaunders UNIX for Dummies Questions & Answers 1 02-26-2004 12:14 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-12-2006
Registered User
 

Join Date: Nov 2006
Posts: 22
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
[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 05:23 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-12-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,465
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 "#"
Reply With Quote
  #3 (permalink)  
Old 12-12-2006
Registered User
 

Join Date: Nov 2006
Posts: 22
Can you explain this further because
Usually it's of the form s/old/new/g but sed 's#/#\\/#g' contains only s/.../g
Reply With Quote
  #4 (permalink)  
Old 12-12-2006
Registered User
 

Join Date: Nov 2006
Posts: 22
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'`
Reply With Quote
  #5 (permalink)  
Old 12-12-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,639
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 $(...)

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-12-2006 at 09:22 PM.
Reply With Quote
  #6 (permalink)  
Old 12-12-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,334
Backtick version:
abc=`echo "a/b/d" | sed 's#/#\\\\/#g'`

but I strongly prefer $(...) to `...` for many reasons.
Reply With Quote
  #7 (permalink)  
Old 12-13-2006
Registered User
 

Join Date: Nov 2006
Posts: 22
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 ..
Reply With Quote
Google UNIX.COM
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0