[ale] [OT] Variation of grep

Robert L. Harris robert.l.harris at gmail.com
Wed Mar 11 18:30:11 EDT 2009


If the format is always these 6 lines:

grep -B4 -A2 STATE: WA <file> > <file.out>


On 3/11/09 4:27 PM, JK wrote:
> 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
>
>    

-- 

:wq!
====================================================================
Robert L. Harris                     | GPG Key ID: E344DA3B
                                          @ x-hkp://pgp.mit.edu
DISCLAIMER:
       These are MY OPINIONS             With Dreams To Be A King,
        ALONE.  I speak for              First One Should Be A Man
        no-one else.                       - Manowar




More information about the Ale mailing list