awk
A general solution for ith pattern block in awk is:
awk -v i=1 -v pat='something P1 something' '$0~pat{i--}i==0'
Explanation:
-v i=1 # sets the pattern block to print (1 in this case).
-v pat='...' # sets the regex pattern that will be tested.
$0~pat # tests if the input line match the pattern
{i--} # If the pattern was found, reduce the count.
i==0 # If the count has reduced to 0, print the block lines.
If the pattern that matters is only P1, then use:
awk -v i=1 -v pat='P1' '$0~pat{i--}i==0'
For a faster execution, exit when the block has ended:
awk -v i=1 -v pat='P1' '$0~pat{i--}i==0;i<0{exit}'
If you want a literal match (not a pattern), use:
awk -v i=1 -v pat='P1' '$0 == pat {i--}; i==0; i<0{exit}'
sed
To get from the first instance of one pattern to the next instance of a pattern, you can do in GNU sed:
sed -n '/something P1 something/!b;b2;:1;{/something P1 something/q;:2;p;n;b1}'
There may be some lines before the first something P1 something.
The script stops (fast) when the second pattern is found.
As both patterns (start and end) are equal, we may reduce the command to:
sed -n -e '/something P1 something/!b;b2;:1;{//q;:2;p;n;b1}'
And to make it more portable, use:
sed -n -e '/something P1 something/!{b' -e '};b2' -e ':1' -e '{//q;:2' -e 'p;n;b1' -e '}'
sed -n '/something P1 something/,/something P1 something/p' input | head -n -1– jesse_b Jan 04 '20 at 18:40head -n -1would be useful, though just in case. – RonJohn Jan 04 '20 at 18:50P1is what I really need, and the Torin answer (which exactly answers my question) was easily modified to only match strings withP1in them. – RonJohn Jan 04 '20 at 19:24P1in them, mine finds lines that match strings withP1in them so you can just pick whichever satisfies whatever your requirements are. – Ed Morton Jan 05 '20 at 20:12