Subject: Re: printing a regular expression match
To: Michael Gorsuch <michael.gorsuch@gmail.com>
From: Jeremy C. Reed <reed@reedmedia.net>
List: netbsd-users
Date: 12/08/2006 13:42:40
> 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...

Try with sed:

   sed -n 's/^.*\(1234\).*$/\1/p'

The -n makes it so the non-matching lines are not printed. While the "p" 
does print the matching lines.