perl 匹配的开始和结束行号

perl start and end line number of match

我有以下 perl 命令:perl -l -0777 -sne 'print $& if /\Q$start_word\E\s*(\{(?:[^{}]++|(?1))*+\})/s' -- -start_word="${start_head}" < $path

它从文件中提取一些文本,但我也很感兴趣提取文本的两行之间。在打印出文本之前,我希望它打印出如下内容:The text is located between lines 3 and 7.

示例输入(start_head="开始"):

Hello World
START{
   here are
    {some lines}
}
more text
down here

预期输出:

The text is located between lines 2 and 5.
START{
   here are
    {some lines}
}

调用 perl 命令后的当前输出不包括 The text is located between lines 2 and 5.

你可以试试这样:

perl -l -0777 -sne 'if (/\Q$start_word\E\s*(\{(?:[^{}]++|(?1))*+\})/s) {($start, $end, $match) = ($-[0], $+[0], $&); print "The text is located between lines ", scalar(() = substr($_, 0, $start) =~ /(\r*\n|\r)/g) + 1 , " and ", scalar( () = substr($_, 0, $end) =~ /(\r*\n|\r)/g) + 1; print $match;}' -- -start_word="${start_head}" < $path

为了清楚起见,我把它写得更详细了。

测试输入(Linux行尾(\n)):

1. Hello World
2. Hello World
3. Hello World
4. Hello World
START{
   here are
    {some lines}
}
more text
down here

输出:

The text is located between lines 5 and 8
START{
   here are
    {some lines}
}

具有 Dos/Windows 行终止符 (\r\n) 的相同输入会产生相同的输出。

如果您确定您的输入文本文件行类型将是 Mac (\r) 或 Linux/Unix (\n) 但绝不是 Dos/Windows (\r\n ) 那么可以这样简化:

perl -l -0777 -sne 'if (/\Q$start_word\E\s*(\{(?:[^{}]++|(?1))*+\})/s) {print "The text is located between lines ", substr($_, 0, $-[0]) =~ tr/[\r\n]// + 1 , " and ", substr($_, 0, $+[0]) =~ tr/[\r\n]+// + 1; print $&;}' -- -start_word="${start_head}" < $path

输出(使用相同的输入):

The text is located between lines 5 and 8
START{
   here are
    {some lines}
}