I'm currently working with a text file that contains the following text blocks:
--------------------------------------
Beginning of block
Text
Random Text
keywordA
Text
End of block
--------------------------------------
--------------------------------------
Beginning of block
Text
Random Text
keywordA
Text
End of block
--------------------------------------
--------------------------------------
Beginning of block
Text
Random Text
keywordD
Text
End of block
--------------------------------------
--------------------------------------
Beginning of block
Text
Random Text
keywordd
Text
End of block
--------------------------------------
The objective is to have egrep detect certain keywords, and if those words exist, I would like to copy the block to another file. So, I'm currently searching with:
if egrep -wi 'keywordA|KeywordB|keywordC' Report
then
echo "Words found!"
else
echo "No words found!"
fi
I was wondering if there is any way to add a follow up action to use sed (for example) to copy the block of text where the words were found.
Expected output, in this example, would be:
--------------------------------------
Beginning of block
Text
Random Text
keywordA
Text
End of block
--------------------------------------
--------------------------------------
Beginning of block
Text
Random Text
keywordA
Text
End of block
--------------------------------------
The file "Report" contains dozens of blocks like this, but not all of them have the keyword. I would like to copy just the ones that do (as demonstrated in the above example).
awk
with settingRS
would be better suited... (also what is your version.. GNU awk, etc) – Sundeep Jun 4 '17 at 14:09awk -v RS= '/keywordA|KeywordB|keywordC/' Report
– Sundeep Jun 4 '17 at 15:17