Find patterns and filter the text

I need to filter the text in between two patterns and output that to a different file. Please help me how to do it.
Ex:
.............
<some random text>
.............
Pattern_1
<Few lines that need to be output to different file>
Pattern_2
................
...............
<more text in the file>
..............

I want to be able to Find Pattern_1, then output the next few lines until I find Pattern_2. Let me know if my request is clear or needs detailed explanation.

Try:

awk '/Pattern_1/,/Pattern_2/' file
1 Like

Here is how to get lines between "Pattern_1" and "Pattern_2" with ex...

ex -s +'/Pattern_1/+1,/Pattern_2/-1 p | q!' file
1 Like

To include the tags (i.e. Pattern_1 and Pattern_2 in the output), bartus11's solution will work for sure...
But if you do not want the tags, you could try:

nawk '/Pattern_2/{ flag=0 } flag; /Pattern_1/{ flag=1 }' input

1 Like

thanks guys...i got what i wanted....