Regular Expression Trouble

Tim Hammerquist penryu at saiyix.ath.cx
Fri Dec 9 22:39:34 PST 2005


Martin McCormick wrote:
> After reading  a bit about extended regular expressions and
> having a few actually work correctly in sed scripts, I tried one in
> egrep and it isn't working although there are no errors.
> 
> I was hoping to get only the A records from a dns zone file so
> the expression I used is:
> 
> egrep [[:space:]IN[:space:]A[:space:]] zone_file >h0

If you're using vi, put your cursor on that very first '[' and
bounce on the % key for a while; see if anything occurs to you.

If not, you could probably use a refresher course on that little
sub-syntax of regular expressions called character classes.

> It seems to match almost everything.

The regex "[[:space:]IN[:space:]A[:space:]]" is composed
entirely of one large character class.  Classes are logical sets
and have no interest in the order or quantity of their contents,
so it's reduced to "[[:space:]AIN]".  When fed to egrep, it
says, "match any line which contains any of these 4 entities".
Most lines will contain a space character, if not the 3 letters,
so your output makes sense.

As the [:space:] only has meaning inside brackets itself, it's
improtant to open enclose each individual occurence inside it's
own additional set of brackets.

egrep "[[:space:]]IN[[:space:]]A[[:space:]]" zone_file >h0

HTH,
Tim Hammerquist


More information about the freebsd-questions mailing list