我正在尝试拆分这些值,我没有达到预期的效果
$line = "sep 11 14:06:18 github-01-com sshd[4609]: Received disconnect from 192.168.1.1: disconnected by user";
if($line =~ /^(.*)\s+(\w+)/){
print "$1\n$2\n";
我正试图获得像这样的输出
$val1 = "sep 11 14:06:18";
$val2 = "github-01-com";
$val3 = "sshd[4609]:"
$val4 = "Received disconnect from 192.168.1.1: disconnected by user"
感谢
答案 0 :(得分:2)
对于此特定行,您可以像这样对其进行解码:
#!/usr/bin/perl
use strict;
use warnings;
my $line = "sep 11 14:06:18 github-01-com sshd[4609]: Received disconnect from 192.168.1.1: disconnected by user";
if ($line =~ m/^(\w+\s+\d+\s+(?:\d|:)+)\s+([^\s]+)\s+([^\s]+)\s+(.*)$/) {
print "\$1 = $1\n\$2 = $2\n\$3 = $3\n\$4 = $4\n";
}
这是输出:
$1 = sep 11 14:06:18
$2 = github-01-com
$3 = sshd[4609]:
$4 = Received disconnect from 192.168.1.1: disconnected by user
顺便说一句,取决于您尝试解析的日志文件类型,可能存在一些模块,尝试在CPAN中搜索它们。