LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-06-2010, 05:55 AM   #1
CartmanYO
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Rep: Reputation: 1
sed and lazy regex


Hello! Here's my question. This command replaces "abb" substring
Code:
echo abbc | sed "s/.*b//g"
And this command doesn't work (lazy regex):
Code:
echo abbc | sed "s/.*?b//g"
Why? This one should replace "ab" substring.
 
Old 10-06-2010, 06:06 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The ? needs to be escaped: echo abbc | sed "s/.*\?b//g"

Hope this helps.
 
Old 10-06-2010, 06:17 AM   #3
CartmanYO
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 1
Thanks for your reply, druuna, but this fix doesn't help - the result of this command is still "c"
 
Old 10-06-2010, 06:24 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What in your mind was the point of the question mark?
 
Old 10-06-2010, 06:26 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This replaces a single character at the beginning of the line (^.) followed by a b with nothing. You'll end up with bc.
echo abbc | sed "s/^.b//"

I'm not sure if this is what your are looking for, but hope it helps.
 
Old 10-06-2010, 06:30 AM   #6
CartmanYO
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 1
Sure, this will work in current case of "abbc". But what if the string is "123 abbc"? I'm interested in lazy regular expressions and i suppose they are bugged in sed.
 
Old 10-06-2010, 06:35 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

What are you trying to accomplish, maybe your approach isn't correct:

echo "1234 abbc" | sed "s/.*b/b/" will leave you with bc, regardless of what is in front of bc.

Hope this helps.
 
1 members found this post helpful.
Old 10-06-2010, 06:36 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
sed does not support non-greediness. To do that, you use [^]

Code:
# echo abbc | sed 's/.*[^b]b//g'
bc
 
1 members found this post helpful.
Old 10-06-2010, 06:41 AM   #9
CartmanYO
LQ Newbie
 
Registered: Jun 2009
Posts: 27

Original Poster
Rep: Reputation: 1
OK, thanks. That was all i need to know.
 
Old 10-06-2010, 06:46 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Are you perhaps trying to catch everything up until the first b and and then also the first b?
try:
Code:
echo "123 abbc" | sed 's/[^b]*b//'
 
  


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
Help with sed regex homer_3 Linux - General 1 08-18-2009 01:57 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
sed regex question ShaqDiesel Programming 1 02-09-2007 07:24 PM
sed / regex question whysyn Linux - General 3 06-28-2005 02:11 PM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 01:09 PM

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

All times are GMT -5. The time now is 11:17 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
Open Source Consulting | Domain Registration