top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

grep by paragraph in Linux

+1 vote
438 views

Is there an option for grep, or some similar program, that will print out a whole paragraph -
defined as the section between two blank lines - containing a given word or phrase?

If not, can anyone suggest a simple script that will do this?

posted Aug 30, 2013 by Amit Parthsarthi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

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
this is a red
this is another green

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

answer Aug 30, 2013 by Sheetal Chauhan
+1 vote

Try grep -p for this.

answer Aug 30, 2013 by Kumar Mitrasen
...