LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-27-2014, 12:07 AM   #1
Almaz
Member
 
Registered: Jul 2013
Posts: 35

Rep: Reputation: Disabled
Need help with Sed regex


I can't find a way to delete everything after a character on the first occurrence.

I have:
first case: blahh123-blah-456 one two three
second case: 123-blah two 23 three

must be converted to

blah-456 one two three
blah two 23 three

so far I came up with the following regex, it only works for the first case but it doesn't work for the second one. I need sed to be able to remove all the characters before and including first sign "-"
Code:
echo "blahh123-blah-456 one two three" | sed 's/\(.*\)-\(.*-\)/\1foo39820\2/' | sed 's/^.*foo39820//'
Please point me to the right directions. Thanks in advance

Last edited by Almaz; 07-27-2014 at 12:12 AM.
 
Old 07-27-2014, 12:12 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,696

Rep: Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261
Wouldn't it be simpler since the desired removal is at the beginning of the line? "s/^blahhh123-//"
 
Old 07-27-2014, 12:19 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,619

Rep: Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088
Only works for that specific case. The generic answer is to delete all non-minus characters up to the (first) minus character.
Character classes allow you define such a case - the doco describes this.
 
Old 07-27-2014, 12:19 AM   #4
Almaz
Member
 
Registered: Jul 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
Wouldn't it be simpler since the desired removal is at the beginning of the line? "s/^blahhh123-//"
it's not constant
 
Old 07-27-2014, 12:27 AM   #5
Almaz
Member
 
Registered: Jul 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
In other words I really need to get this cleaned up. That's what I have

/tmp/var/log/messages.0-Jul 27 00:22:49 go.sonobi.com
/tmp/var/log/messages-Jul 27 00:22:49 kona.kon-tera.com
/tmp/var/log/messages.0-Jul 27 00:22:49 pagead2.googlesyndication.com
/tmp/var/log/messages-Jul 27 00:22:49 s.skimresources.com

I'm trying to make it look clean

Jul 27 00:22:49 go.sonobi.com
Jul 27 00:22:49 kona.kon-tera.com
Jul 27 00:22:49 pagead2.googlesyndication.com
Jul 27 00:22:49 s.skimresources.com
 
Old 07-27-2014, 12:50 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,619

Rep: Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088
If one was slightly twisted this would work on that data - using a character class you could reduce it to 7 chars and make it completely independent of the actual text. Like I said, see the doco.
Code:
sed -r 's:^/tmp/var/log/messages(.0)*-::' some.input
 
Old 07-27-2014, 01:03 AM   #7
Almaz
Member
 
Registered: Jul 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
Thanks for all your help. It looks like I was trying to make it more complicated than it's really is. I just used a very basic method instead of complicated one

Code:
|sed 's/^.*messages[.]0-//' | sed 's/^.*messages-//'

Last edited by Almaz; 07-27-2014 at 01:06 AM.
 
Old 07-27-2014, 02:28 AM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Wouldn't it be simpler to use 'cut'? I haven't tested it but you could try:

Code:
cut -d'-' -f2- file
 
Old 07-27-2014, 02:39 AM   #9
Almaz
Member
 
Registered: Jul 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
Thanks a lot sycamorex and syg00 both methods works great. Never used cut option before but it's good to know now. I might even use it in the future.
 
  


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
Regex or Sed grob115 Programming 6 10-02-2012 09:00 PM
sed regex one or none '?' casperdaghost Linux - Newbie 5 06-21-2012 09:44 AM
[SOLVED] Regex with sed pixiandreas Linux - Newbie 12 05-16-2012 02:30 PM
[SOLVED] sed regex schneidz Programming 1 02-28-2011 06:46 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM


All times are GMT -5. The time now is 06:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration