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".

0

2 Answers 2

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
0
perl -00ne 'print if /TOO/'
  • -00 means paragraph mode (records are separated by one or more empty lines).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.