grep by paragraph

Dave Mitchell davem at iabyn.com
Fri Aug 30 09:38:23 UTC 2013


On Fri, Aug 30, 2013 at 10:50:31AM +0200, Timothy Murphy wrote:
> that will print out a whole paragraph -
> defined as the section between two blank lines -
> containing a given word or phrase?
> 
> Such as the above 4 lines.
> If not, can anyone suggest a simple script that will do this?

The perl one-liner below demonstrates this. Setting the $/ (input record
separator) var to the empty string causes perl to read in "lines" a
paragraph at a time.

    $ cat /tmp/text
    this is a
    green paragraph


    this is a
    red
    paragraph

    this
    is another
    green
    paragraph

    Rhubarb.

    $ perl -e'$/=""; while (<>) { print if /green/ }' /tmp/text
    this is a
    green paragraph

    this
    is another
    green
    paragraph

    $

-- 
"Strange women lying in ponds distributing swords is no basis for a system
of government. Supreme executive power derives from a mandate from the
masses, not from some farcical aquatic ceremony."
    -- Dennis, "Monty Python and the Holy Grail"


More information about the users mailing list