top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

grep by paragraph in Linux

+1 vote
81 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 Google+ Share Button LinkedIn Share Button Multiple Social 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

Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...