grep by paragraph

Dave Cross davorg at gmail.com
Fri Aug 30 10:18:47 UTC 2013


On 30 August 2013 10:38, Dave Mitchell <davem at iabyn.com> wrote:

> 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.
>
>     $ perl -e'$/=""; while (<>) { print if /green/ }' /tmp/text


You can use Perl's command line options to clean that up a bit.

$  perl -00 -ne 'print if /green/' para.txt

-00 is the same as $/=""
-n is the same as "while (<>) { YOUR CODE HERE }"

See "perldoc perlrun" (http://perldoc.perl.org/perlrun.html) for details.

-- 
Dave Cross :: dave at dave.org.uk
http://dave.org.uk/
@davorg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.fedoraproject.org/pipermail/users/attachments/20130830/d54fe238/attachment.html>


More information about the users mailing list