My goal is to extract the paragraphs of a text that contain a specific keyword. Not just the lines that contain the keyword, but the whole paragraph. The rule imposed on my text files is that every paragraph starts with a certain pattern (e.g. Pa0) which is used throughout the text only in the start of the paragraph. Each paragraph ends with a new line character.
For example, imagine I have the following text:
Pa0
This is the first paragraph bla bla bla
This is another line in the same paragraph bla bla
This is a third line bla bla
Pa0
This is the second paragraph bla bla bla
Second line bla bla My keyword is here!
bla bla bla
bla
Pa0
Hey, third paragraph bla bla bla!
bla bla
Pa0
keyword keyword
keyword
Another line! bla
My goal is to extract these paragraphs that contain the word "keyword". For example:
Pa0
This is the second paragraph bla bla bla
Second line bla bla My keyword is here!
bla bla bla
bla
Pa0
keyword keyword
keyword
Another line! bla
I can use e.g. grep for the keyword and -A, -B or -C option to get a constant number of lines before and/or after the line where the keyword is located but this does not seem enough since the beginning and end of the text block depends on the delimiters "Pa0" and "\n".
Any suggestion for grep
or another tool (e.g. awk, sed, perl) would be helpful.
hope this will help
sed -n '/Pa0/,/^$/p' filename
cat filename | sed -n '/Pa0/,/^$/p'
-n, suppress automatic printing of pattern space
-p, Print the current pattern space
/Pa0/, paragraph starting with Pa0 pattern
/^$/, paragraph ending with a blank line
^, start of line
$, end of line
Reference: http://www.cyberciti.biz/faq/sed-display-text/
if text.txt contains the text you want, then:
It is simple with
awk
:Explanation:
Usually awk operates on a per line basis, because the default value of the record separator
RS
is\n
(a single new line). By changing theRS
to two new lines in sequence (an empty line) we can easily operate on a paragraph basis./keyword/
is a condition, a regex. Since there is no action after the conditionawk
will simply print the unchanged record (the paragraph) if it containskeyword
.Setting the output record separator
ORS
to\n\n
will separate the paragraphs of output with an empty line, just like in the input.