LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-11-2015, 09:49 PM   #31
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750

Hey syg00,
how are you ?

So you're saying the first '.*' effectively causes it to match the last occurrence?
I did a quick couple of tests and that seems to be the case.
How would you specify the first match then?
(this is worrying; I used to be ok at regexes...)
 
Old 11-11-2015, 09:55 PM   #32
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,118

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Yep - it matches the entire text. Then the regex engine starts working backwards until the next regex element matches.
Rinse, shake, repeat.

Of course, regex ain't regex.
 
Old 11-11-2015, 09:58 PM   #33
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
True; I need to re-read Friedl's book. Funnily enough I was planning to revise that today, hence the interest.
So (as per my last qn) how would you force it to match 1st occurrence instead?


EDIT: oh yeah; just started doing it in Perl : with the front & back wildcards you get the last one; without wildcards you get the first.

Still working on the sed version (I always was a bit iffy with sed...)

Last edited by chrism01; 11-11-2015 at 10:31 PM. Reason: s/see/sed/ ... sigh
 
Old 11-11-2015, 10:25 PM   #34
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,118

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Sorry - forgot that q.
You can't in sed. You need non-greedy quantifiers, which last I looked sed doesn't support them. perlre is simplest soln.
 
Old 11-11-2015, 11:10 PM   #35
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
That explains why I'm banging my head on the desk.... its so nice to stop
 
Old 11-12-2015, 02:01 AM   #36
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,772

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
what about this?
sed -r -n 's/11:([0-9]+).*/11:\1/;s/.*11://p'

Last edited by pan64; 11-12-2015 at 02:22 AM.
 
Old 11-12-2015, 02:04 AM   #37
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,118

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
You can't generalise it for an indeterminate number of occurrences of the required string (in sed)
 
Old 11-12-2015, 02:23 AM   #38
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,772

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
obviously not, but can find first occurence
 
Old 11-12-2015, 10:36 AM   #39
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,774

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by syg00 View Post
You can't generalise it for an indeterminate number of occurrences of the required string (in sed)
Never say, "It can't," with sed. It's language is, after all, Turing complete. Given enough time and resources, it can do anything any computer could ever do. Whether it is a practical candidate for a given application is a different question.
 
Old 11-12-2015, 11:38 AM   #40
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Talking

Quote:
Originally Posted by rknichols View Post
Never say, "It can't," with sed. It's language is, after all, Turing complete. Given enough time and resources, it can do anything any computer could ever do. Whether it is a practical candidate for a given application is a different question.
Your assignment: Rewrite the Linux kernel in sed.
 
Old 11-12-2015, 05:27 PM   #41
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Talking

Now that would impress even Linus
 
  


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
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 04:36 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
[SOLVED] awk regex /f[eo]{2}t/ does not work as advertised Telengard Programming 14 07-24-2010 01:43 AM
APACHE: AliasMatch not work, regex not right? tclwp Linux - Software 1 03-16-2005 01:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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