[SOLVED] [sed] Returns whole string instead of part
Linux - SoftwareThis 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
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.
well yes you saying replace that part of the string with itself (minus the square brackets). You need to also match whatever is at the start and whatever is at the end.
/tmp# echo "before [myfile.txt] after" | sed -e 's/^.*\[\(.*\)\]*.$/\1/'
Last edited by acid_kewpie; 07-03-2011 at 11:02 AM.
Actually I was interpreting it wrong, it's not working as it's not matching. To that end I've always really struggled to remember / know why .+ never works here. IF the .+ was working, then the pattern would match and then be giving the output I anticipated:
# echo "before [myfile.txt] after" | sed 's/\[\(.*\)\]/\1/'
before myfile.txt after
It does, but that is mental... what's the flag to properly interpret the +?
It's mental, I know. But it's in the sed manual. Check "info sed" section 3.3 "Overview of Regular Expression Syntax". Don't know (yet) if there's a flag to interpret + without escaping.
Ok, I got it. It's extended regular expressions. From the sed info page:
Quote:
Appendix A 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_.
So the regex would be:
Code:
echo "before [myfile.txt] after" | sed -r 's/.*\[(.+)\].*/\1/'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.