I want to grab multiple lines in a file with a pattern that has a starting and an ending tag in an ungreedy way.For example I have the following input:file.txtSTARTtest1test2fooENDsomemore textSTARTtest3bartest4test5ENDeven moreSTARTbaztest6ENDNow I want to look for bar and print out everything between START and END, so that I will get:STARTtest3bartest4test5ENDWhat I have so far is the following grep command:grep -Pzo '(?s)START.*?bar.*?END' file.txtThe problem is, that this expression is greedy and prints out:START      # starts at first START-tag, not the next onetest1      #test2      #foo        #END        #some       #more text  #STARTtest3bartest4test5ENDIt's not done with the grep flags --before-context / --after-context, because the count of lines before and after can differ.The tool used by text-processing doesn't matter. It should work on a general RedHat system. Also the faster the tool grabs the lines, the better it will be. Because I have big logfiles of each about 150MB.Can someone tell me, how to achieve my goal the best way?Update:Okay, I got it. I just had to think about how to construct my command from don_crisstis link. Here's the solution:ed -s file.txt <<< $'g/bar/?START?,/END/p\nq\n'Thank you very much for all your very fast help!And yes, finally it's a duplicate...
grab text by pattern with start and end tag in multiple lines
text processing;regular expression