LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 11:10 AM   #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, 11:39 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
Old 02-12-2009, 11:39 AM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
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).
 
1 members found this post helpful.
Old 02-12-2009, 02: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, 02:23 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can use a simple parameter substitution instead of sed:
Code:
$ string="blah-blah-blah-123"
$ string=${string%-*}
$ echo $string
blah-blah-blah
 
1 members found this post helpful.
Old 02-12-2009, 06: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-12-2009, 11:58 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 12:44 PM   #8
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

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

Jan
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

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