在perl中读取日志文件,并在方括号中找到值

时间:2019-01-25 13:09:30

标签: regex perl

我有一个logfile.txt,其中包含某个测试用例的执行日志。该文件的示例如下所示。尽管它还有其他几行,但是为了简单起见,我将其删除。

在此logfile.txt中,我的最终目标是在最后一行的方括号内获取十六进制值。

我已经在PERL中编写了一个示例代码来搜索最后一行并打印,但是我无法制定正则表达式来获取所需的值。

我的代码:

#!/usr/bin/perl

my $logfile = "/path/to/logfile.txt";
open ( IN1 , $logfile) or die "Cannot find the file $logfile \n";
while (<IN1>)
    {
        chomp ($_);
        print $_ if ($_ =~ m/The job has completed/i);
    }
close (IN1);

输入

   > cat logfile.txt

Waiting for the completion of job..
.
.
.
.
.


The job has completed, returning [0x0]

输出

0x0

3 个答案:

答案 0 :(得分:4)

类似这样的东西:

#!/usr/bin/perl

use strict;
use warnings;

open my $log_fh, '<', 'logfile.txt' or die $!;

while (<$log_fh>) {
  print $1 if /The job has completed.+\[(.+)\]/;
}

我已经使用括号((...))“捕获”了[]之间出现的任何内容。当正则表达式匹配时,捕获的文本将存储在$1中。

注意,我还更新了您的代码,以使用更现代的习惯用法来打开文件。我a)使用词法文件句柄,b)使用open()的三参数版本,并且c)检查open()的返回值,并在失败时终止程序。

答案 1 :(得分:4)

要匹配包括句子开头在内的最后一行,可以使用:

^The job has completed,.*?\[\K0[xX][0-9a-fA-F]+(?=\])

Regex demo

这将匹配:

  • ^The job has completed,从字符串开头按字面意义进行匹配
  • .*?匹配任何不贪心的字符
  • \[\K匹配左括号,而忘记使用\K匹配的内容
  • 0[xX][0-9a-fA-F]+匹配一个十六进制数字
  • (?=\])积极前瞻,以确认后面的内容是一个结束符

答案 2 :(得分:0)

尝试使用此Perl单线版

console.js:35 ERROR Error: Uncaught (in promise): ReferenceError: Hls is not defined
ReferenceError: Hls is not defined
    at VgHLS.webpackJsonp.1185.VgHLS.createPlayer (vg-hls.js:59)
    at VgHLS.webpackJsonp.1185.VgHLS.ngOnChanges (vg-hls.js:47)
    at checkAndUpdateDirectiveInline (core.js:12092)
    at checkAndUpdateNodeInline (core.js:13598)
    at checkAndUpdateNode (core.js:13541)
    at debugCheckAndUpdateNode (core.js:14413)
    at debugCheckDirectivesFn (core.js:14354)
    at Object.eval [as updateDirectives] (LiveEventVideo.html:73)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:14339)
    at checkAndUpdateView (core.js:13508)
    at callViewAction (core.js:13858)
    at execComponentViewsAction (core.js:13790)
    at checkAndUpdateView (core.js:13514)
    at callWithDebugContext (core.js:14740)
    at Object.debugCheckAndUpdateView [as checkAndUpdateView] (core.js:14277)
    at ViewRef_.detectChanges (core.js:11300)
    at NavControllerBase._viewAttachToDOM (nav-controller-base.js:460)
    at NavControllerBase._transition (nav-controller-base.js:540)
    at nav-controller-base.js:261
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4629)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (core.js:4620)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)
    at e.invokeTask [as invoke] (polyfills.js:3)
    at p (polyfills.js:2)
    at IDBRequest.v (polyfills.js:2)
    at c (polyfills.js:3)
    at Object.reject (polyfills.js:3)
    at NavControllerBase._fireError (nav-controller-base.js:223)
    at NavControllerBase._failed (nav-controller-base.js:216)
    at nav-controller-base.js:263
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4629)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3
    at t.invokeTask (polyfills.js:3)
    at Object.onInvokeTask (core.js:4620)
    at t.invokeTask (polyfills.js:3)
    at r.runTask (polyfills.js:3)
    at o (polyfills.js:3)
    at e.invokeTask [as invoke] (polyfills.js:3)
    at p (polyfills.js:2)
    at IDBRequest.v (polyfills.js:2)

更短..谢谢@Dave Cross

$ perl -lne ' if(eof) { /.*\[(.*)\].*/ and print "Hex = $1" } ' logfile.txt
Hex = 0x0
$

如果要确保最后一行中的“作业已完成”,则

$ perl -lne ' if(eof) { /\[(.*)\]/ and print "Hex = $1" } ' logfile.txt
Hex = 0x0
$