regex:在npp中选择多行[已关闭]

wkftcu5l  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(112)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question

更新:我找到了解决方案的正则表达式。“(?s)信息:初始流量。?ID 123。?信息:Init flow”就可以了!

我有一个日志文件,其结构如下:

20:43:36.452 [18856] [1] start trace line 1
20:43:36.455 [18856] [1] start trace line 2
20:43:36.569 [18856] [1] start trace line 3
20:44:01.749 [18856] [1] INFO : Init flow
20:44:01.764 [18856] [1] DEBUG: line 
20:44:01.860 [18856] [1] DEBUG: line 
20:44:01.861 [18856] [1] DEBUG: ID123
20:44:01.864 [18856] [1] DEBUG: line
20:44:02.009 [18856] [1] DEBUG: line
20:44:31.969 [18856] [1] INFO : Init flow
20:44:31.974 [18856] [1] DEBUG: line 
20:44:31.975 [18856] [1] DEBUG: line 
20:44:32.029 [18856] [1] DEBUG: ID456
20:44:32.030 [18856] [1] DEBUG: line
20:44:32.039 [18856] [1] DEBUG: line
20:44:32.109 [18856] [1] INFO : Init flow
20:44:32.109 [18856] [1] DEBUG: line 
20:44:32.109 [18856] [1] DEBUG: line 
20:44:32.179 [18856] [1] DEBUG: ID789
20:44:32.179 [18856] [1] DEBUG: line
20:44:32.179 [18856] [1] DEBUG: line
20:44:32.289 [18856] [1] INFO : Init flow

是否可以从行“INFO:初始化流”,通过ID到“INFO:初始流量”?
例如,如果我在ID 123上搜索,我希望选择以下行:

20:44:01.749 [18856] [1] INFO : Init flow
20:44:01.764 [18856] [1] DEBUG: line 
20:44:01.860 [18856] [1] DEBUG: line 
20:44:01.861 [18856] [1] DEBUG: ID123
20:44:01.864 [18856] [1] DEBUG: line
20:44:02.009 [18856] [1] DEBUG: line
20:44:31.969 [18856] [1] INFO : Init flow

我尝试了多个regex示例和网站https://regex101.com/
我对惰性搜索(向后看和向前看)感到困惑。

huus2vyu

huus2vyu1#

如果您保证只需要前面和后面各2行,则可以使用Grep:

grep -A2 -B2 'ID123' <file>

然而,如果你需要做更多的逻辑,我不推荐使用多行正则表达式或文本编辑器,相反,我推荐使用日志解析器。

my $target = shift; # grab the first command line argument as the target ID
                    # protip: supports regex symantics so ID12[34] would get ID123 and ID124
my @lines = (); # buffer
my $print = false; # set to true if you want to print the target buffer
while (<>) { # read all input line by line into $_
    if /Init Flow/ {
        print @lines if $print;  # print the buffer with target content
        @lines = (); # flush lines
        $print = false; # printed; look for next target
    }
    if /$target/ {
        $print = true; # we found our target
    }
    push(@lines, $_); # add the current line to the buffer
}

可以通过在命令行上提供目标来运行此示例:

find_messages.pl ID123 logfile.log
mdfafbf1

mdfafbf12#

我找到了这个正则表达式的解:(?s)信息:初始化流程。?ID 123。?信息:初始流量
谢谢您的建议!

相关问题