Subject: Re: printing a regular expression match
To: Christian Biere <christianbiere@gmx.de>
From: Mike M. Volokhov <mishka@netbsd.org>
List: netbsd-users
Date: 12/08/2006 19:53:19
Christian Biere <christianbiere@gmx.de> wrote:
> Michael Gorsuch wrote:
> > Hey all, I'm trying to find the right Unix tool to print out a matched
> > regular expression, not just a line that matches.
> > 
> > For example,
> > 
> > echo [1234] | egrep \(1234\)
> > 
> > It matches the expression, but I only want to print the match, '1234'.
> 
> I don't know which shell you use but I'd recommend to use quotes to prevent
> clashes with shell meta-characters:
> 
> echo '[1234]' | egrep '\(1234\)' | sed 's,^.*\(1234\).*$,\1,'

Also, it's possible to use perl regex - $& will contain matched string.

> I hope I didn't just do your homework because I hate homework.

Or you love homework, or homework loves you :-)

--
Mishka