0

I have a sample file below:

# This is a test file. This is a test file. This is a test file  
This is a test file. This is a test file. This is a test file.  
This is a test file.

# Need to output just this paragraph.Need to output just this paragraph.  
Need to output just this paragraph TOO. Need to output just this paragraph.  
Need to output just this paragraph.

I need to output just the second paragraph starting from "#" till the last sentence in the paragraph.

How do I grep and output based on the pattern? Say if the file has more paragraphs, and I would like to output the paragraph which contains the word "TOO".

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227

2 Answers2

2

If the paragraphs are empty line separated:

awk -v RS= -v ORS='\n\n' /TOO/

An empty record separator (RS) means paragraph mode where records are separated by sequences of empty lines.

If they are # separated:

awk -v RS='#' '/TOO/ {print RS $0}'

or

pcregrep -Mo '#[^#]*?TOO[^#]*'
  • -M for multiline grep
  • -o to output the matched portion only
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
0
perl -00ne 'print if /TOO/'
  • -00 means paragraph mode (records are separated by one or more empty lines).
JJoao
  • 11,887
  • 1
  • 22
  • 44