LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   simple substitution with sed? (http://www.linuxquestions.org/questions/programming-9/simple-substitution-with-sed-622838/)

ocicat 02-21-2008 01:25 PM

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.

dive 02-21-2008 02:00 PM

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

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

ocicat 02-21-2008 03:09 PM

Quote:

Originally Posted by dive (Post 3065274)
Not sure why yours isn't working but this seems to:

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

Sweet! Thanks!

pixellany 02-21-2008 03:43 PM

Oops--I misread your first example. sorry
I simply MUST remember to take my meds......;)

David the H. 02-21-2008 09:41 PM

(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

Tinkster 02-22-2008 08:49 PM

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

ghostdog74 02-22-2008 11:56 PM

Code:


a=$1
case $a in
./* ) s=${a/\.} ;;
esac
echo $s

output:
Code:

# ./test.sh ./blogger
/blogger


jschiwal 02-23-2008 12:22 AM

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)"

Tinkster 02-23-2008 12:28 AM

Quote:

Originally Posted by jschiwal (Post 3066888)
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

jschiwal 02-23-2008 12:45 AM

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.



All times are GMT -5. The time now is 08:04 PM.