LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-03-2004, 05:04 AM   #1
nexus55
LQ Newbie
 
Registered: May 2004
Posts: 5

Rep: Reputation: 0
problem with sed in a bash script


Heya guys:

I've got a small problem with the usage of sed..

This works perfectly from the command line:

Code:
echo "ftps://user:pass@some.site.com:port/path" | sed 's/\//\\\//g'
(returns ftps:\/\/user:pass@some.site.com:port\/path as it should)

but when used in a bash script I can't get it to work properly:

if $1 is ftps://user:pass@some.site.com:port/path , then

Code:
R_PATH=`echo "$1" | sed 's/\//\\\//g'`
with that code, $R_PATH should be ftps:\/\/user:pass@some.site.com:port\/path but it doesn't work :(

any suggestions would be appreciated ;)
 
Old 05-03-2004, 08:27 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
from bashref.info:
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
substitution. When using the `$(COMMAND)' form, all characters between
the parentheses make up the command; none are treated specially.

I tried out your example myself using the "$( command )" form of command substitution.
Code:
$ cat test2
#!/bin/bash
export RPATH=$(echo "ftp://ftp.host/user" | sed 's/\//\\\//g')
echo $RPATH
echo
$ . test2
ftp:\/\/ftp.host\/user

$ echo $RPATH
ftp:\/\/ftp.host\/user
$
 
Old 05-03-2004, 08:47 AM   #3
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
ignore this post, I said something that didn't make sense

Last edited by rkef; 05-03-2004 at 08:52 AM.
 
Old 05-03-2004, 12:30 PM   #4
nexus55
LQ Newbie
 
Registered: May 2004
Posts: 5

Original Poster
Rep: Reputation: 0
yep the "$( command )" method solved part of my problem indeed, thanks for posting it ;)

another sed-related problem with the same script though, is this:

Code:
#in the below example, $1 is: ftps://user4:pass4@2.2.2.2:34/pppp
  
RPATH_CUR=$(cat $RCF_FILE|grep help.conf\"\;|awk -F \" '{print $2}')
#$RPATH_CUR returns: ftps://user2:pazz5392588@some.2dfre6.com:port/path/help.conf                 
  
RPATH_STRIPPED=${RPATH_CUR%/*}
RPATH_NEW=$(echo $1 | sed 's/\//\\\//g')

sed "s/$RPATH_STRIPPED/$RPATH_NEW/g" < $RCF_FILE > $RCF_FILE.sed
everything works up until the last sed statement, which is where sed returns an error:

Code:
sed: -e expression #1, char 10: Unknown option to 's'
thanks in advance to anyone who can help ;)

Last edited by nexus55; 05-03-2004 at 12:50 PM.
 
Old 05-03-2004, 07:31 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
All of these '/'s are confusing me, and apparently sed.

To make things a little less obtuse, use this form instead:

Code:
RPATH_NEW=$(echo $1 | sed 's#/#\\/#g')

sed "s#$RPATH_STRIPPED#$RPATH_NEW#g" $RCF_FILE >$RCF_FILE.sed
After variable expansion, the sed command contains all those forward slashes which you are using as the separation character. Now you'd have a problem if '#'s are used.

Last edited by jschiwal; 05-03-2004 at 07:35 PM.
 
Old 05-03-2004, 08:43 PM   #6
nexus55
LQ Newbie
 
Registered: May 2004
Posts: 5

Original Poster
Rep: Reputation: 0
excellent! that worked!

I didn't know different separation characters could be used with sed, but that has fixed the remaining problems..

thank you again jschiwal!
 
Old 05-03-2004, 09:40 PM   #7
FinalFantasy
Member
 
Registered: Sep 2003
Distribution: Debian
Posts: 49

Rep: Reputation: Disabled
: (colon) and # (hash) are another two seperators avaiable besides / (forword slash).
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem on sed. chooi Programming 3 02-10-2006 11:35 PM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 07:07 AM
bash script problem sorry99 Programming 2 09-20-2004 09:02 AM
bash Problem with "sed" command blubbfish Linux - Newbie 3 06-01-2004 10:14 AM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration