On Fri, Aug 30, 2013 at 11:54:20AM -0500, Michael Hennebry wrote:
On Fri, 30 Aug 2013, Timothy Murphy wrote:
>Dave Mitchell 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
>
>Thanks for the solutions.
>
>I gather there is no method using grep or one of its variants?
sed could do it.
I don't have a recipe on me at the moment.
Try this:
$ sed -e '/[^[:space:]]\+/{H;d}' -e 'x;/<search_string>/!d'
path_to_file.txt
This has the side-effect of adding a newline in the front. You can
partially deal with it by using `1h;1!H' instead of `H' in the first
expression; but it only works if the match is in the 1st paragraph. I
do not think it is possible to deal with it properly in sed.
Cheers,
PS: Modified version of an example from this article:
<
http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-7-examples-for-sed-...
--
Suvayu
Open source is the future. It sets us free.