用perl解析文本文件

用perl解析文本文件,perl,Perl,我有一个文本文件如下。 我需要将关键字查找为“(number)tests from(number)test cases run”,并存储该编号。 在下面的例子中,数字是67。 其次,我需要找到“PASSED”关键字,然后后跟“(number)tests”。 在以下情况下,是67次测试。我该怎么做呢 坦克, 沙拉斯

我有一个文本文件如下。 我需要将关键字查找为“(number)tests from(number)test cases run”,并存储该编号。 在下面的例子中,数字是67。 其次,我需要找到“PASSED”关键字,然后后跟“(number)tests”。 在以下情况下,是67次测试。我该怎么做呢

坦克, 沙拉斯

<0x00000:5b 2d 5d 20 31 34 20[------------]14
<0x00010:DISAB的74 65 73 73 20 66 72 6f 6d 20 44 49 53 41 42测试
<0x00020:4c 45 44 5f 47 65 6e 65 72 69 63 44 52 4d 54 65 LED一般指示灯
<0x00030:73 74 20 28 31 32 38 34 39 20 6d 73 20 74 6f 74 st(12849 ms tot
<0x00040:61 6c 29 0d 0a 0d 0a 5b 2d al)[--------
<0x00050:2d 2d 5d 20 47 6c 6f 62 61 6c 20 74 65 73 74 20--]全局测试
<0x00060:65 6e 76 69 72 6f 6e 6d 65 6e 74 20 74 65 61 72环境撕裂
<0x00070:2d 64 6f 77 6e 0d 0a 5b 3d-向下[========
<0x00080:3d 5d 20 36 37 20 74 65 74 73 20 66 72 6f==]67测试
<0x00090:6d 20 34 20 74 65 73 74 20 63 61 73 65 73 20 72 m 4测试用例r
<0x000a0:61 6e 2e 20 28 34 38 32 33 20 6d 73 20 74 6f an。(43823毫秒至
<0x000b0:74 61 6c 29 0d 0a 5b 20 50 41 53 45 44 20总计)【通过】
<0x000c0:20 5d 20 36 37 20 74 65 73 2e 0d 0a]67测试。。。
我尝试了以下代码,但不匹配:

open (FILE, '<', 'board_dumplog.log')
    or die "Could not open board_dumplog.log: $!";

while (<FILE>) {
    #print $_ if (/^[==========]/ .. /^tests.../);
    if (/^[==========]/ .. /^tests.../) {
        print "Line Found:".$_."\n";
    }
}

close (FILE)
    or die "Could not close board_dumplog.log: $!";

open(文件,您需要正确地转义特殊字符,或者应该在regexp中使用/Q/E修饰符来匹配

 if (/^\[[\=]+\]\s*(\d+)\s*tests/i) {
   print "found $1 tests in line: $_\n";

如果要匹配正则表达式中的文字字符串,则需要转义正则表达式特殊字符

您应该了解这些特殊字符是什么,但如果您想要快捷方式,可以使用或
\Q..\E

if (/^\Q[==========]\E/ .. /^\Qtests...\E/) {

括号
[]
将不再被视为字符类,句点
将不再是任何字符。

我收到错误:bash-3.2$perl 123.pl反斜杠在123.pl第72行附近的“/^[\=]+]/\(在\?之前缺少运算符)123.pl第72行附近的语法错误/^[[\=]+]/\“替换未在123.pl第72行终止。找到的行包括[-----------],而不是仅[=======]。使用十六进制代码编辑器显示数据是不必要的复杂。由于您正在逐行处理,我建议您将数据显示为简单的文本文件。
 if (/^\[[\=]+\]\s*(\d+)\s*tests/i) {
   print "found $1 tests in line: $_\n";
if (/^\Q[==========]\E/ .. /^\Qtests...\E/) {