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 07-03-2011, 10:49 AM   #1
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Rep: Reputation: 35
Question [sed] Returns whole string instead of part


Hello

I'm not used to using "sed", but I need to use it to extract part of a string piped to it.

However, the "\1" mentioned in the tutorials I read doesn't extract the token between the two brackets and returns the whole input string:
Code:
/tmp# echo "before [myfile.txt] after" | sed 's/\[\(.+\)\]/\1/'                                                      before [myfile.txt] after
I expected simply "myfile.txt".

Does someone know why?

Thank you.
 
Old 07-03-2011, 10:58 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 07-03-2011, 10:58 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Try the following:
Code:
$ echo "before [myfile.txt] after" | sed 's/.*\[\(.*\)\].*/\1/'
 
Old 07-03-2011, 11:06 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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
 
Old 07-03-2011, 11:16 AM   #5
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Original Poster
Rep: Reputation: 35
Thanks guys. It's strange that sed doesn't seem to use "+" as usual, ie. "one or more characters".
 
Old 07-03-2011, 11:44 AM   #6
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by littlebigman View Post
Thanks guys. It's strange that sed doesn't seem to use "+" as usual, ie. "one or more characters".
The + has to be escaped:

Code:
echo "before [myfile.txt] after" | sed 's/.*\[\(.\+\)\].*/\1/'
Returns myfile.txt.
 
Old 07-03-2011, 11:49 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
It does, but that is mental... what's the flag to properly interpret the +?
 
Old 07-03-2011, 11:53 AM   #8
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by acid_kewpie View Post
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.
 
Old 07-03-2011, 12:11 PM   #9
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
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/'
Returns myfile.txt using an unescaped +.
 
Old 07-04-2011, 07:07 AM   #10
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Original Poster
Rep: Reputation: 35
Thanks for the tip on "-r".
 
  


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
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
[SOLVED] C - How to put a specific arbitrary part of a string into it's own string? golmschenk Programming 9 04-19-2010 08:27 PM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
extracting part of a string (sed, bash, ...) rama76 Programming 3 01-11-2008 08:14 AM

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

All times are GMT -5. The time now is 12:57 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