0

亲爱的,我正在编写一个 python 程序,它从 .gz 文件中检索 edifact 日志消息... 2 个日志的示例如下:

2009/03/02 12:13:59.642396 siamp102 mux1-30706 Trace name: MSG
Message sent [con=251575 (APEOBEinMux1), len=2106, CorrID=000182C42DE0ED]
UNB+IATB:1+1ASRPFA+1A0APE+090302:1213+0095JQOL2

2009/03/02 12:14:00.029496 siamp102 mux1-30706 Trace name: MSG
Message sent [con=737 (APIV2_1), len=22370, CorrID=000182C42DE0ED]
UNB+IATB:1+1ASIFQLFS+1ARIOFS+090302:1214+0122V11ON9

我想编写一个正则表达式,能够匹配第一行中的某些字段,第二行中的某些字段和第三行中的某些字段...

有什么方法可以编写一个正则表达式与 GREP 一起使用,以匹配连续行中的字段?

提前致谢 !!!

4

2 回答 2

1

检查this previous thread,您可能会得到您正在寻找的答案:bash grep newline

查看pcregrep答案,pcregrep -M允许多行匹配。

于 2012-07-07T09:25:45.960 回答
0

独自一人,我grep认为这是不可能的。我建议awkperl为了能够从前几行中保存一些上下文。

perl这给出了类似的东西:

#!/usr/bin/env perl

$isInLogSection = 'NO';
while (<>) {
    if ( /siamp102/ ) {
        # Start of log section: retrieve its ID
        $isInLogSection = 'YES';
        split;
        $logSectionID = $_[0];
    }

    if ($isInLogSection eq YES && /len=/) {
        # Retrieve value of len
        ...
    }

    if ( /^$/ ) {
        # End of log section
        $isInLogSection = 'NO';
    }
}

awk这给出了类似的东西:

BEGIN { isInLogSection = "NO"; }
/siamp102/ { isInLogSection = "YES"; logSectionID = $1; }
/len=/ { if (isInLogSection == "YES") { #retrieve len value } }
/^$/ { isInLogSection = "NO" }

我不是 100% 确定确切的语法。这主要是说明原理的画布。

于 2009-03-11T10:29:48.213 回答