LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices



Reply
 
Search this Thread
Old 02-12-2009, 12:10 PM   #1
mr_scary
Member
 
Registered: Aug 2006
Posts: 39

Rep: Reputation: 15
sed - last occurence of a match


Using sed, how do I remove everything after the last dash (-) in a string?

So how do I turn

'blah-blah-123' or 'blah-blah-blah-123'

into

'blah-blah' or 'blah-blah-blah'

respectively?

--
mr. scary
 
Old 02-12-2009, 12:39 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Code:
echo 'blah-blah-blah-123'| sed "s/-[^-]*$//"
Matches a hyphen, then any number of characters except a hyphen, until the end of the line. This means that only the last occurrence of the hyphen satisfies the string.
 
Old 02-12-2009, 12:39 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

regular expressions are greedy (they catch the longest part of a line, which matches the pattern), so this will work:
Code:
jan@jack:~/tmp> echo 'blah-blah-blah-123' | sed -r 's/(.*)-.*/\1/'
blah-blah-blah
Jan

P.S.: The only one exception from the "greedy behaviour" is the "?" quantifier in Perl compatible regular expressions (not supported by grep and sed).
 
Old 02-12-2009, 03:21 PM   #4
mr_scary
Member
 
Registered: Aug 2006
Posts: 39

Original Poster
Rep: Reputation: 15
Thank you jan61. That works (your explanation is a nice bonus). I did, however, need to escape the parentheses.
 
Old 02-12-2009, 03:23 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can use a simple parameter substitution instead of sed:
Code:
$ string="blah-blah-blah-123"
$ string=${string%-*}
$ echo $string
blah-blah-blah
 
Old 02-12-2009, 07:22 PM   #6
mr_scary
Member
 
Registered: Aug 2006
Posts: 39

Original Poster
Rep: Reputation: 15
Ha. Changed to the parameter substitution solution instead. Great solutions guys.
 
Old 02-13-2009, 12:58 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Awww, I almost mentioned the parameter substitution thing myself, but decided against it because the OP specifically asked for a sed solution. Since substitution requires the use of a variable, it's less convenient than sed when doing things like working with a text file, and since the purpose of the change wasn't mentioned I decided not to presume.

This is a good example of why questions like this really ought to be goal-centered: "how can I accomplish x", rather than process-centered: "how can I make y do x".


PS: If you use the -r switch in sed you don't need to escape the parentheses.
 
Old 02-13-2009, 01:44 PM   #8
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

did you use the -r option (extended regex)?

Jan
 
  


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
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 10:23 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 03:31 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 09:55 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 02:47 PM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 09:34 AM


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

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