Re: gnu grep -o flag

2010-03-25 Thread Denis Doroshenko
On Wed, Mar 24, 2010 at 11:13 PM, Philip Guenther guent...@gmail.com wrote:
 On Wed, Mar 24, 2010 at 1:06 PM, Philip Guenther guent...@gmail.com
wrote:
 ...
 Hmm, missing quote, and the expressions can be combined, but as a
 portable solution this is indeed the right answer.
 B  B sed -n -e 's/.*\(PATTERN\).*/\1/p'

 The 'portable' solution that doesn't have those problems is to use a
 nuke^W^Wperl:
 B  B perl -nle 'while(m((PATTERN))g){print $1}'

ahem,  is perl really everything these days?
and how portable that is?
is awk completely disregarded as a usable tool?

awk '{ s = $0; while (match(s,PATTERN)) { print substr(s, RSTART,
RLENGTH); s = substr(s, RSTART + RLENGTH); } }'

this gives me the same results are your perl string
(which is shorter of course, after all it's perl).
it also gives the same results are grep -o on
a debian system i have access to.

also awk uses, AFAIK, ERE, in which case
the greediness may be controlled.



Re: gnu grep -o flag

2010-03-25 Thread Chris Dukes
On Wed, Mar 24, 2010 at 09:10:48PM -0500, Ed Ahlsen-Girard wrote:
 
 I'm sure there is a case in which sed should be used instead of perl,
 but I haven't run across it yet.

I've encountered two cases over the past 20 years.
1) Perl is not installed and probably will never be installed.
That ceased to be the case about 10 years ago, and then started
being the case again as folks started replacing the firmware
on consumer grade routers.

2) The time to load perl from disk and the memory consumed doing
so would create a significant load on the system if run every minute.
But that was in the days of perl4.
But in the end I opted for the awk implementation.  The implementation
was readable :-).

And that leads to my gripe...
What is a case of something where sed should be used instead of
awk or some iteration of var2=${var1##*/} and related constructs.
I do know that the use of var2=`echo $var1 | sed -e 's,^.*/,,` 
has a high correlation with gems like
if [ -z $var ]; then ...


-- 
Chris Dukes



Re: gnu grep -o flag

2010-03-24 Thread Jan Stary
On Mar 24 19:00:06, Gregory Edigarov wrote:
 Hello Everybody,
 
 Just wonder how could one implement what gnu grep -o flag does using
 our toolchain? 
 
 from ggrep(1):
 
  -o, --only-matching
   Show  only the part of a matching line that matches
   PATTERN.

Hint: what does g/re/p stand for?



Re: gnu grep -o flag

2010-03-24 Thread Christopher Zimmermann
On Wed, 24 Mar 2010 19:00:06 +0200 Gregory Edigarov wrote:

 Hello Everybody,
 
 Just wonder how could one implement what gnu grep -o flag does using
 our toolchain? 
 
 from ggrep(1):
 
  -o, --only-matching
   Show  only the part of a matching line that matches
   PATTERN.
 
 


maybe try this:
sed -n -e 's/.*\(PATTERN\).*/\1/ -e /PATTERN/p



Re: gnu grep -o flag

2010-03-24 Thread Marco Peereboom
huh?

didn't you just grep for that?

On Wed, Mar 24, 2010 at 07:00:06PM +0200, Gregory Edigarov wrote:
 Hello Everybody,
 
 Just wonder how could one implement what gnu grep -o flag does using
 our toolchain? 
 
 from ggrep(1):
 
  -o, --only-matching
   Show  only the part of a matching line that matches
   PATTERN.
 
 
 -- 
 With best regards,
   Gregory Edigarov



Re: gnu grep -o flag

2010-03-24 Thread Brad Tilley
No.

i...@iso2:~/Desktop$ grep import IDS_targets.py 
import MySQLdb
import socket
import getpass
import datetime

i...@iso2:~/Desktop$ grep import -o IDS_targets.py 
import
import
import
import


On Wed, 24 Mar 2010 13:33 -0500, Marco Peereboom sl...@peereboom.us
wrote:
 huh?
 
 didn't you just grep for that?
 
 On Wed, Mar 24, 2010 at 07:00:06PM +0200, Gregory Edigarov wrote:
  Hello Everybody,
  
  Just wonder how could one implement what gnu grep -o flag does using
  our toolchain? 
  
  from ggrep(1):
  
   -o, --only-matching
Show  only the part of a matching line that matches
PATTERN.
  
  
  -- 
  With best regards,
  Gregory Edigarov



Re: gnu grep -o flag

2010-03-24 Thread Ted Unangst
On Wed, Mar 24, 2010 at 1:00 PM, Gregory Edigarov
g...@bestnet.kharkov.ua wrote:
 Just wonder how could one implement what gnu grep -o flag does using
 our toolchain?

With a 10 line patch.



Re: gnu grep -o flag

2010-03-24 Thread Philip Guenther
On Wed, Mar 24, 2010 at 10:17 AM, Christopher Zimmermann
madro...@zakweb.de wrote:
 On Wed, 24 Mar 2010 19:00:06 +0200 Gregory Edigarov wrote:
 Just wonder how could one implement what gnu grep -o flag does using
 our toolchain?

 from ggrep(1):

  -o, --only-matching
   Show  only the part of a matching line that matches
   PATTERN.

 maybe try this:
 sed -n -e 's/.*\(PATTERN\).*/\1/ -e /PATTERN/p

Hmm, missing quote, and the expressions can be combined, but as a
portable solution this is indeed the right answer.
sed -n -e 's/.*\(PATTERN\).*/\1/p'

If you need extended (egrep-style) regexps, then the most portable
solution is a chunk of awk (left as an exercise for the student); the
less-portable-but-works-in-4.7 solution is to use -E option to sed:
sed -n -E 's/.*(PATTERN).*/\1/p'


Philip Guenther



Re: gnu grep -o flag

2010-03-24 Thread Christopher Zimmermann
On Wed, 24 Mar 2010 13:06:12 -0700 Philip Guenther wrote:

 On Wed, Mar 24, 2010 at 10:17 AM, Christopher Zimmermann
 madro...@zakweb.de wrote:
  On Wed, 24 Mar 2010 19:00:06 +0200 Gregory Edigarov wrote:
  Just wonder how could one implement what gnu grep -o flag does using
  our toolchain?
 
  from ggrep(1):
 
   -o, --only-matching
Show  only the part of a matching line that matches
PATTERN.
 
  maybe try this:
  sed -n -e 's/.*\(PATTERN\).*/\1/ -e /PATTERN/p
 
 Hmm, missing quote, and the expressions can be combined, but as a
 portable solution this is indeed the right answer.
 sed -n -e 's/.*\(PATTERN\).*/\1/p'

right. This one looks nicer.

 If you need extended (egrep-style) regexps, then the most portable
 solution is a chunk of awk (left as an exercise for the student); the
 less-portable-but-works-in-4.7 solution is to use -E option to sed:
 sed -n -E 's/.*(PATTERN).*/\1/p'

sed -E !?! Great! Now I know why I upgraded to -current.



Re: gnu grep -o flag

2010-03-24 Thread Philip Guenther
On Wed, Mar 24, 2010 at 1:06 PM, Philip Guenther guent...@gmail.com wrote:
...
 Hmm, missing quote, and the expressions can be combined, but as a
 portable solution this is indeed the right answer.
sed -n -e 's/.*\(PATTERN\).*/\1/p'

Actually, there are two bug in that, an obvious one and a subtle one.
The obvious one is that it only prints one match per line instead of
printing all the matches.  The subtle one is that if the pattern can
match in multiple overlapping positions, it will report the match with
the *latest* start instead of the *earliest* start (because the
leading .* is greedy).

The 'portable' solution that doesn't have those problems is to use a
nuke^W^Wperl:
perl -nle 'while(m((PATTERN))g){print $1}'


Philip Guenther



Re: gnu grep -o flag

2010-03-24 Thread Marco Peereboom
Congratulations you found import!

On Wed, Mar 24, 2010 at 02:52:31PM -0400, Brad Tilley wrote:
 No.
 
 i...@iso2:~/Desktop$ grep import IDS_targets.py 
 import MySQLdb
 import socket
 import getpass
 import datetime
 
 i...@iso2:~/Desktop$ grep import -o IDS_targets.py 
 import
 import
 import
 import
 
 
 On Wed, 24 Mar 2010 13:33 -0500, Marco Peereboom sl...@peereboom.us
 wrote:
  huh?
  
  didn't you just grep for that?
  
  On Wed, Mar 24, 2010 at 07:00:06PM +0200, Gregory Edigarov wrote:
   Hello Everybody,
   
   Just wonder how could one implement what gnu grep -o flag does using
   our toolchain? 
   
   from ggrep(1):
   
-o, --only-matching
 Show  only the part of a matching line that matches
 PATTERN.
   
   
   -- 
   With best regards,
 Gregory Edigarov



Re: gnu grep -o flag

2010-03-24 Thread Ed Ahlsen-Girard
On Wed, Mar 24, 2010 at 2010-03-24 21:13:40, Philip Guenther
guent...@gmail.com wrote: ...

  Hmm, missing quote, and the expressions can be combined, but as a
  portable solution this is indeed the right answer.
 sed -n -e 's/.*\(PATTERN\).*/\1/p'

 Actually, there are two bug in that, an obvious one and a subtle one.
 The obvious one is that it only prints one match per line instead of
 printing all the matches.  The subtle one is that if the pattern can
 match in multiple overlapping positions, it will report the match with
 the *latest* start instead of the *earliest* start (because the
 leading .* is greedy).

 The 'portable' solution that doesn't have those problems is to use a
 nuke^W^Wperl:
 perl -nle 'while(m((PATTERN))g){print $1}'


 Philip Guenther


I'm sure there is a case in which sed should be used instead of perl,
but I haven't run across it yet.
-- 

Edward Ahlsen-Girard
Ft Walton Beach, FL