Displaying only data matching a pattern?

kenta kenta at guster.net
Mon Feb 2 13:18:00 EST 2009


On Mon, Feb 2, 2009 at 1:06 PM, Shawn O'Shea <shawn at eth0.net> wrote:
> You could try it this way in gawk:
> $ cat foo
> foofoo:A1234567890B\barbar
> foofoo:C9234567890E\barbar
> foofoo:A8234567890B\barbar
> foofoo:F7234567890D\barbar
> $ gawk --posix '{ if (match($0,/[[:xdigit:]]{12}/)) print
> substr($0,RSTART,RLENGTH) }' foo
> A1234567890B
> C9234567890E
> A8234567890B
> F7234567890D
>
>
> You need --posix for [:xdigit:] and the curly braces ( {} ) to work. This
> basically says:
> if the line ($0) matches 12 hexdigits ([:xdigit:]) in a row:
>    print a substring of the line ($0) starting at RSTART and going for
> RLENGTH characters
> match() sets RSTART and RLENGTH for you on a match.
>
> -Shawn

Tested here and this does what I'm looking for, and importantly works
with the gnu-port of gawk for win32.  I did have to change the single
quotes to double quotes for some reason, but it's working.

Thanks to everyone for helping out!

Definitely beats my multiple sed steps.

-Kenta


More information about the gnhlug-discuss mailing list