regex - grep between two strings if pattern in the middle linux -


i want grep between 2 strings if there pattern between them. example, in text: first wanted string start, second end, , pattern 1 2 3 each in new line.

start abc abc 1 2 3 abc end bla bla start abc abc 1 2 4 abc end bla bla start abc abc 1 2 3 abc abc end 

the result should be:

start abc abc 1 2 3 abc end  start abc abc 1 2 3 abc abc end 

thanks!

sed -ne '/start/{:a;n;/end/!b a;/\n1\n2\n3\n/p}' 

line line:

we need text starting 'start':

sed -ne '/start/{ 

we found 'start', add 'end' pattern space;

set label named 'a':

         :a 

add next line pattern space:

         n 

if not found 'end' - jump 'a'

         /end/!b 

now check if have desired pattern contain 1 2 3 , print

they separated '\n' on separate lines

         /\n1\n2\n3\n/p          }' 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -