LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2012, 07:46 AM   #1
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Rep: Reputation: Disabled
Sed Delete:


Code:
sed 's/The.*-//g' udp

udp is:
Code:
The weather is - nice but rainy - cold


I wish this to delete the first - and what before:
Code:
 nice but rainy - cold
? Currently it deletes the second - and what before, and results:
Code:
 cold

Last edited by ali2011; 01-15-2012 at 07:49 AM.
 
Old 01-15-2012, 07:59 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can use [^-]* instead of .* and drop the global flag.
 
1 members found this post helpful.
Old 01-15-2012, 08:07 AM   #3
ali2011
Member
 
Registered: Nov 2011
Location: USA, CA
Distribution: Ubuntu+Fedora
Posts: 80

Original Poster
Rep: Reputation: Disabled
Thanks a lot for the quick and helpful response. Cheers!
 
Old 01-15-2012, 09:37 PM   #4
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
To give more detail, many regular expression functions like "*" and "+" are greedy. That is, they always consume the longest possible match. So ".*-" means everything up to the last hyphen in the string. The pattern jschiwal gave is a common work-around; match everything that's not a hyphen, so that the first hyphen encountered satisfies the expression.

And don't forget that the "g" in sed is for "global" matches, it will do the replacement on every match of the pattern in the string. Leave it off to only replace the first one. BTW, a little-known feature is that you can also use a number to specify which match on the line to replace. e.g. sed 's/[^-]*-//2' will delete the part between the two hyphens (and the second hyphen).


If your string is stored in a shell variable, you can use a simple parameter substitution instead. Unlike regex, globbing patterns aren't naturally greedy.

Code:
line='The weather is - nice but rainy - cold'
echo "${line#*- }"
 
  


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
[SOLVED] Help sed delete before and after tuxtutorials Linux - Software 2 01-04-2011 06:40 PM
delete lines with sed LuciusHunt Programming 1 04-19-2008 02:40 PM
Delete a line using sed kushalkoolwal Programming 2 09-29-2007 07:25 PM
trying to delete a line with sed deoren Linux - General 2 01-03-2005 09:26 PM
cannot delete certain files with sed ganninu Linux - General 6 12-10-2003 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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