The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 05:51 AM
Converting \0 to a \n ajcannon Shell Programming and Scripting 2 09-28-2007 11:30 AM
Converting seeyou SCO 3 01-19-2006 02:22 PM
converting .txt laila63 UNIX for Dummies Questions & Answers 4 06-30-2004 04:56 PM
converting kb to mb csaunders UNIX for Dummies Questions & Answers 1 02-26-2004 04:14 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 3.00 average. Display Modes
  #1 (permalink)  
Old 12-12-2006
sakthi.abdullah sakthi.abdullah is offline
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 09:23 AM..
  #2 (permalink)  
Old 12-12-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
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 (permalink)  
Old 12-13-2006
sakthi.abdullah sakthi.abdullah is offline
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
  #4 (permalink)  
Old 12-13-2006
sakthi.abdullah sakthi.abdullah is offline
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'`
  #5 (permalink)  
Old 12-13-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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-13-2006 at 01:22 AM..
  #6 (permalink)  
Old 12-13-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,100
Backtick version:
abc=`echo "a/b/d" | sed 's#/#\\\\/#g'`

but I strongly prefer $(...) to `...` for many reasons.
  #7 (permalink)  
Old 12-14-2006
sakthi.abdullah sakthi.abdullah is offline
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 ..
Sponsored Links
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 03:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0