This question already has an answer here:
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:
START test1 test2 foo END some more text START test3 bar test4 test5 END even more START baz test6 END
Now I want to look for bar and print out everything between START and END, so that I will get:
START test3 bar test4 test5 END
What I have so far is the following grep command:
grep -Pzo '(?s)START.*?bar.*?END' file.txt
The problem is, that this expression is greedy and prints out:
START # starts at first "START"-tag, not the next one test1 # test2 # foo # END # some # more text # START test3 bar test4 test5 END
It'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?
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...