LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-21-2008, 12:25 PM   #1
ocicat
Member
 
Registered: May 2007
Posts: 207

Rep: Reputation: 46
simple substitution with sed?


This is simple, but I'm not resolving it. The following should simply remove the leading dot.
Code:
$ cat script.sh
#!/bin/sh
echo "$1"
s=$(echo "$1" | sed 's!^\.(.*$)!$1!')
echo "$s"
$ sh script.sh "./booger"
./booger
./booger
$
Any insight would be appreciated. Thanks.
 
Old 02-21-2008, 01:00 PM   #2
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,203

Rep: Reputation: 292Reputation: 292Reputation: 292
Not sure why yours isn't working but this seems to:

s=$(echo "$1" | sed 's!^\.!!')
 
Old 02-21-2008, 02:09 PM   #3
ocicat
Member
 
Registered: May 2007
Posts: 207

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by dive View Post
Not sure why yours isn't working but this seems to:

s=$(echo "$1" | sed 's!^\.!!')
Sweet! Thanks!
 
Old 02-21-2008, 02:43 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Oops--I misread your first example. sorry
I simply MUST remember to take my meds......

Last edited by pixellany; 02-21-2008 at 02:45 PM.
 
Old 02-21-2008, 08:41 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
(Caveat before I start: I'm still pretty much a sed and regex beginner. I may be very mistaken on some things.)

When using your original, it appears that you need to escape the parentheses or the shell fails to read it correctly. As it is, sed isn't matching anything, so your output is the unmodified echo command. Also, even when that's fixed, the '$1' in the replacement field doesn't work. It's being perceived as a literal string. You need to use \1 if you want to insert only the text in the parentheses.

This works for me.

#!/bin/sh

echo "$1"
s=$(echo "$1" | sed 's!^\.\(.*$\)!\1!')
echo "$s"


david$ ./script.sh ./booger
./booger
/booger
 
Old 02-22-2008, 07:49 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Yah. The parenthesis need to be escaped unless you turn on
extended regex mode ... not for the shell though, but for sed.

Code:
echo ./booger | sed -r 's!^\.(.*$)!\1!'
/booger

Cheers,
Tink
 
Old 02-22-2008, 10:56 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Code:
a=$1
case $a in
./* ) s=${a/\.} ;;
esac
echo $s
output:
Code:
# ./test.sh ./blogger
/blogger

Last edited by ghostdog74; 02-22-2008 at 11:31 PM.
 
Old 02-22-2008, 11:22 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I believe that the patterns "\(" and "\)" are used to save a regular expression match into a register. If extended regex is enabled, then
they are used unescaped to group regular expressions rather than to save the matching pattern into a register.
"(pattern1|pattern2)"
 
Old 02-22-2008, 11:28 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by jschiwal View Post
I believe that the patterns "\(" and "\)" are used to save a regular expression match into a register. If extended regex is enabled, then
they are used unescaped to group regular expressions rather than to save the matching pattern into a register.
"(pattern1|pattern2)"
Hmm ... my working example above kind of contradicts that
theory, doesn't it?


Cheers,
Tink
 
Old 02-22-2008, 11:45 PM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
My bad. I guess I was thinking about egrep.
Code:
Extended regular expressions
****************************

   The only difference between basic and extended regular expressions
is in the behavior of a few characters: `?', `+', parentheses, and
braces (`{}').  While basic regular expressions require these to be
escaped if you want them to behave as special characters, when using
extended regular expressions you must escape them if you want them _to
match a literal character_.

Examples:
`abc?'
     becomes `abc\?' when using extended regular expressions.  It
     matches the literal string `abc?'.

`c\+'
     becomes `c+' when using extended regular expressions.  It matches
     one or more `c's.

`a\{3,\}'
     becomes `a{3,}' when using extended regular expressions.  It
     matches three or more `a's.

`\(abc\)\{2,3\}'
     becomes `(abc){2,3}' when using extended regular expressions.  It
     matches either `abcabc' or `abcabcabc'.

`\(abc*\)\1'
     becomes `(abc*)\1' when using extended regular expressions.
     Backreferences must still be escaped when using extended regular
     expressions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Simple sed problem need help Silvermask Linux - Software 2 04-12-2007 01:15 PM
sed substitution with p flag 7stud Linux - Newbie 2 03-03-2007 04:15 AM
Command substitution and sed daYz Linux - General 9 11-04-2006 01:15 AM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 12:36 AM
sed substitution error BlkPoohba Programming 1 08-25-2004 02:00 PM


All times are GMT -5. The time now is 09:03 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration