I am looking to get all the lines which have the word 'search_string' + the line after it + the line matching 'mod' before it.
I tried:
grep -n 'mod\|search_string' ip | grep --before 1 search_string> inter
grep -n --after 1 search_string ip >> inter
sort -t':' -k1,1n -u inter -o op
Is there a better way?
File:
mod start1
some lines
mod start2
other lines
mod start3
many other lines
search_string yada yada
hello
many other lines
search_string yada yada
bye
mod start4
search_string baba baba
this too
mod start5
Expected output :
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
Best Answer
Explanation:
mod
is saved asmd
.search_string
triggers printing the previously savedmd
, the line itself and the next line.if(md!="")
andmd=""
are there to make sure you don't get duplicatedmod
lines when there are manysearch_string
-s under a singlemod
(mod start3
in your example).Note:
mod
andsearch_string
will break this logic.