2015-08-21 63 views
-1

我試圖從我用系統命令創建的文件中獲取某些值。該文件是按順序和正則表達式正在工作,直到我到達「換行」。我試圖讓它以多種方式獲得其他價值,但我似乎無法弄清楚。我哪裏錯了?Perl正則表達式dotall問題

下面是代碼

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 <[email protected]>: Recipient address rejected: User unknown in local recipient table; from=<[email protected]> to=<[email protected]> 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] 

子程序返回數組哈希

[ 
    { servicename => "sshd.service", servicestatus => "running" }, 
    { servicename => "cups.service", servicestatus => "running" }, 
    { servicename => "ntpd.service", servicestatus => "dead" }, 
    { servicename => "named.service", servicestatus => "running" }, 
    { servicename => "postfix.service", servicestatus => "running" }, 
] 
+1

好的,那有什麼問題? – melpomene

+0

當你逐行閱讀時,在'$ line'的正則表達式中使用's'修飾符沒有什麼意義.. –

+0

'[a-z] + .ser''應該是'[a-z] + \ .service'。 – melpomene

回答

0

我會嘗試讀取的文件將答案寫入var,然後使用split處理令牌(似乎令牌之間有空行) e:

open(F,「<」,file)||死 」...」; {local $ /;在$ =; }#啜食文件

的foreach $線(分割(/ \ n \ n /,在$)) { 如果($線=〜/([az]+.service)\s-.*(running |死)/秒){ ...... }