Perl正则表达式dotall问题

时间:2015-08-21 17:55:28

标签: regex linux perl

我正在尝试从使用系统命令创建的文件中获取某些值。该文件是有序的,正则表达式正在运行,直到我到达“换行符”。我试图让它以多种方式获取其他价值,但我似乎无法弄明白。我哪里错了?

这是代码

sealed trait TestEnum { def getName }
case object Foo extends TestEnum { def getName = "foo" }
case object Bar extends TestEnum { def getName = "bar" }

这是我正在解析的文件

sub servicechoise2 {

    my $sys_com = "Servicestatus.txt";

    print "type status you would like to see status of: ";
    my $service = <>;
    chomp $service;

    system( "systemctl status $service > $sys_com" );

    open( my $fh2, "<", $sys_com );

    my @services;

    while ( my $line = <$fh2> ) {

        if ( $line =~ /([a-z]+.service)\s-.*(running|dead)/s ) {

            my %hash2 = (
                "servicename"   => $1,
                "servicestatus" => $2
            );

            push( @services, \%hash2 );
        }
    }

    return \@services;
}

子程序返回这个散列数组

sshd.service - OpenSSH server daemon    Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)    Active: active (running) since Fri 2015-08-21 18:20:06 CEST; 1h 32min ago  Main PID: 1297 (sshd)    CGroup: /system.slice/sshd.service
           └─1297 /usr/sbin/sshd -D

Aug 21 18:20:06 Thomas-PC systemd[1]: Started OpenSSH server daemon. Aug 21 18:20:07 Thomas-PC sshd[1297]: Server listening on 0.0.0.0 port
22. Aug 21 18:20:07 Thomas-PC sshd[1297]: Server listening on :: port 22.

cups.service - CUPS Printing Service    Loaded: loaded (/usr/lib/systemd/system/cups.service; enabled)    Active: active (running) since Fri 2015-08-21 18:20:33 CEST; 1h 32min ago  Main PID: 3657 (cupsd)    CGroup: /system.slice/cups.service
           └─3657 /usr/sbin/cupsd -f

Aug 21 18:20:33 Thomas-PC systemd[1]: Started CUPS Printing Service.

ntpd.service - Network Time Service    Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled)    Active: inactive (dead)


named.service - Berkeley Internet Name Domain (DNS)    Loaded: loaded (/usr/lib/systemd/system/named.service; enabled)    Active: active (running) since Fri 2015-08-21 18:20:10 CEST; 1h 32min ago   Process: 2477 ExecStart=/usr/sbin/named -u named $OPTIONS (code=exited, status=0/SUCCESS)   Process: 1302 ExecStartPre=/usr/sbin/named-checkconf -z /etc/named.conf (code=exited, status=0/SUCCESS)  Main PID: 2502 (named)    CGroup: /system.slice/named.service
           └─2502 /usr/sbin/named -u named

Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.biz/A/IN': 2001:503:7bbb:ffff:ffff:ffff:ffff:ff7e#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.biz/AAAA/IN': 2001:503:7bbb:ffff:ffff:ffff:ffff:ff7e#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.biz/A/IN': 2001:500:3682::12#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.biz/AAAA/IN': 2001:500:3682::12#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'ns2.isc.ultradns.net/A/IN': 2001:502:4612::e8#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.com/AAAA/IN': 2001:502:f3ff::e8#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.com/AAAA/IN': 2610:a1:1016::e8#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.co.uk/AAAA/IN': 2610:a1:1017::e8#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.co.uk/A/IN': 2610:a1:1017::e8#53 Aug 21 19:20:11 Thomas-PC named[2502]: error (network unreachable) resolving 'pdns196.ultradns.biz/A/IN': 2610:a1:1015::e8#53

postfix.service - Postfix Mail Transport Agent    Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled)    Active: active (running) since Fri 2015-08-21 18:20:10 CEST; 1h 32min ago   Process: 1335 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 1328 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)   Process: 1298 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)  Main PID: 2531 (master)    CGroup: /system.slice/postfix.service
           ├─2531 /usr/libexec/postfix/master -w
           ├─2534 pickup -l -t unix -u
           └─2535 qmgr -l -t unix -u

Aug 21 18:20:06 Thomas-PC systemd[1]: Starting Postfix Mail Transport Agent... Aug 21 18:20:09 Thomas-PC postfix/postfix-script[2510]: warning: group or other writable: /etc/postfix/./main.cf Aug 21 18:20:10 Thomas-PC postfix/postfix-script[2529]: starting the Postfix mail system Aug 21 18:20:10 Thomas-PC postfix/master[2531]: daemon started -- version 2.10.1, configuration /etc/postfix Aug 21 18:20:10 Thomas-PC systemd[1]: Started Postfix Mail Transport Agent. Aug 21 18:23:08 Thomas-PC postfix/smtpd[4293]: connect from localhost[127.0.0.1] Aug 21 18:23:08 Thomas-PC postfix/smtpd[4293]: NOQUEUE: reject: RCPT from localhost[127.0.0.1]: 550 5.1.1 <a14thona@localhost>: Recipient address rejected: User unknown in local recipient table; from=<admin@localhost> to=<a14thona@localhost> proto=ESMTP helo=<localhost.localdomain> Aug 21 18:23:08 Thomas-PC postfix/smtpd[4293]: lost connection after RCPT from localhost[127.0.0.1] Aug 21 18:23:08 Thomas-PC postfix/smtpd[4293]: disconnect from localhost[127.0.0.1]

1 个答案:

答案 0 :(得分:0)

我会尝试将答案读入var,然后使用split处理标记(似乎标记之间有空行),例如:

打开(F,“&lt;”,文件)||死 ”...”; {local $ /;在$ =; } #slurp文件

foreach $ line(split(/ \ n \ n /,$ in)) {   if($ line =〜/([a-z]+.service)\s-.*(running|dead)/s) {  ...... }