You can try something like this:
perl -l -0777 -sne 'if (/Q$start_wordEs*({(?:[^{}]++|(?1))*+})/s) {($start, $end, $match) = ($-[0], $+[0], $&); print "The text is located between lines ", scalar(() = substr($_, 0, $start) =~ /(
*
|
)/g) + 1 , " and ", scalar( () = substr($_, 0, $end) =~ /(
*
|
)/g) + 1; print $match;}' -- -start_word="${start_head}" < $path
I made it slightly more verbose for clarity.
Test input (Linux end of lines (
)):
1. Hello World
2. Hello World
3. Hello World
4. Hello World
START{
here are
{some lines}
}
more text
down here
Output:
The text is located between lines 5 and 8
START{
here are
{some lines}
}
The same input with Dos/Windows line terminators (
) do produce the same output.
If you are sure your input text file lines types will be either Mac (
) or Linux/Unix (
) but never Dos/Windows (
) then it can be simplified like this:
perl -l -0777 -sne 'if (/Q$start_wordEs*({(?:[^{}]++|(?1))*+})/s) {print "The text is located between lines ", substr($_, 0, $-[0]) =~ tr/[
]// + 1 , " and ", substr($_, 0, $+[0]) =~ tr/[
]+// + 1; print $&;}' -- -start_word="${start_head}" < $path
Output (with the same input):
The text is located between lines 5 and 8
START{
here are
{some lines}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…