[ale] [OT] Variation of grep

JK jknapka at kneuro.net
Wed Mar 11 18:27:18 EDT 2009


Hi folks,

I wrote a little Python program to do the following task, which
I seem to need frequently. But I wonder if there's an existing
utility that does it; Google has revealed nothing of interest.
The task is this:

I've got a file with some line-oriented structure to it. Maybe
it's formatted XML, or something like.  I want to identify
delimited sections of the file containing lines that match
a particular regular expression, and write the matching
sections (not just the matching lines) to standard output.
For example, I have a file:

CUSTOMER Doe-12345
   NAME: John Doe
   ADDRESS: 123 4th st
   CITY: Walla Walla
   STATE: WA
/CUSTOMER

CUSTOMER Jones-23456
   NAME: Jane Jones
   ADDRESS: 5 6th Ave
   CITY: Nowhere
   STATE: TX
/CUSTOMER

and I want to write out the CUSTOMER../CUSTOMER sections for
all customers from Washington state. My tool lets me do:

   mgrep.py --start="CUSTOMER" --end="/CUSTOMER" --target="STATE: WA" filename

(where any of those strings could be regexps); and the output is
the whole CUSTOMER section for John Doe, as well as for any other
customers that match the target.

Is there already such a tool, or was I actually not reinventing any
simple machines with this?

(Bring on the perl one-liners...)

-- JK

-- 
A closed mouth gathers no feet.


More information about the Ale mailing list