Subject: Re: printing a regular expression match
To: Michael Gorsuch <michael.gorsuch@gmail.com>
From: Greg A. Woods <woods@weird.com>
List: netbsd-users
Date: 12/08/2006 18:28:43
At Fri, 8 Dec 2006 10:06:46 -0500,
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'.
> 
> Any ideas?  I'm sure there's a unix tool I'm not using properly here...

There's always AWK for these things (my favourite in many cases):

	awk '{match($0, /1234/); print substr($0, RSTART, RLENGTH);}'

of course AWK can also do the sub() trick in the same way as with sed.

(any interpreted add-on language supporting REs will likely work too)

-- 
						Greg A. Woods

H:+1 416 218-0098 W:+1 416 489-5852 x122 VE3TCP RoboHack <woods@robohack.ca>
Planix, Inc. <woods@planix.com>       Secrets of the Weird <woods@weird.com>