ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
(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"
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)"
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.