gpt4 book ai didi

regex - Perl 正则表达式 dotall 问题

转载 作者:太空宇宙 更新时间:2023-11-04 05:25:51 28 4
gpt4 key购买 nike

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

这是代码

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]

子例程返回此哈希数组

[
{ 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" },
]

最佳答案

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

打开(F,"<",文件) ||死 ”...”;{ 本地 $/; $在=; } # slurp 文件

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

关于regex - Perl 正则表达式 dotall 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32146562/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com