[ale] [OT] Variation of grep

Jim Kinney jim.kinney at gmail.com
Wed Mar 11 18:44:29 EDT 2009


assuming a repeating structure as in the example:

grep -A2 -B4 "STATE: WA"

should print all the CUSTOMER../CUSTOMER lines with STATE: WA





On Wed, Mar 11, 2009 at 6:27 PM, JK <jknapka at kneuro.net> 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
>
> --
> A closed mouth gathers no feet.
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
>



-- 
-- 
James P. Kinney III



More information about the Ale mailing list