我有一个awk命令,它将存储如下结果,从结果我想传递参数并获取我的记录有3个条件。
awk'/ ^ E 20 [0-9] [0-9] / {d = $ 1 FS $ 2 FS $ 3; f = 1; n = NR} f&& / location / $ 1 = $ 1; gsub(/ [<>] /,“”); print n,d,$ 0; f = 0}'OFS ='|' logfile>>错误日志
输入错误日志文件将如下所示
1434887|E 2014-04-10 23:07:02.288|location=PIN_ERRLOC_PCP:2|class=PIN_ERRCLASS_SYSTEM_DET:1|errno=PIN_CONNECT_FAILED:114
1434896|E 2014-04-10 23:07:02.288|location=Unknown|pin|location:0|class=UNKNOWN:0|errno=PIN_CONNECT_FAILED:26
1434947|E 2014-04-10
答案 0 :(得分:0)
您可以尝试以下Perl脚本:
#! /usr/bin/perl
use v5.12;
use File::Slurp qw(read_file);
use Getopt::Long;
use Time::Piece;
my ($date,$time)=getOpts();
my @lines=read_file('errorlog');
if (defined $date && ! defined $time) {
for (@lines) {
print if $date eq @{[split(" ")]}[1];
}
}
elsif (defined $date && defined $time) {
for (@lines) {
my $tmp=@{[split(/\|/)]}[1];
my ($a,$b)=$tmp=~/E (.*?) (.*)\./;
print if ($date eq $a && $time le $b);
}
}
elsif (defined $time) {
$date = localtime->strftime("%Y-%m-%d");
for (@lines) {
my $tmp=@{[split(/\|/)]}[1];
my ($a,$b)=$tmp=~/E (.*?) (.*)\./;
print if ($date eq $a && $time le $b) || ($date lt $a);
}
}
sub getOpts {
my $date;
my $time;
GetOptions (
"date=s" => \$date,
"time=s" => \$time
);
return ($date,$time);
}
示例运行:p.pl --date=2014-04-03 --time=15:07:15
答案 1 :(得分:0)
我已经通过简单的shell脚本找到了解决方案,并且工作正常
/ usr / bin / env bash
INPUT_FILE = “input.txt中”
awk'/ ^ E / {a = substr($ 0,1,24); n = NR} NR == n + 2&& n {print n,a,$ 0}'logfile> “$ INPUT_FILE”
如果[$(echo $ 1 | wc -m)-gt 11 -a“x $ {2}”==“x”];然后 TS = $ 1 其他 日期= $ 1 TS = $ 16 网络
if [“x $ {ts}”!=“x”-a“x $ {date}”!=“x”];然后
grep ${date} ${input_file} | sed -n '/'"${ts}"'/,$p'
elif [“x $ {ts}”!=“x”-a“x $ {date}”==“x”];然后
sed -n '/'"${ts}"'/,$p' ${input_file}
否则
grep ${date} ${input_file}
网络连接