如何打印日志文件直到特定日期

时间:2013-09-26 06:48:31

标签: perl scripting

我有格式的cron日志文件 当前日期是sep 22

9月22日13:00:01主机名crond [24359] :( root)CMD(/ usr / lib64 / sa / sa1 1 1)

我想用以下格式打印日志文件,直到“sep 21”

date:time:user:command format

2 个答案:

答案 0 :(得分:3)

怎么样:

perl -pe 'exit if /^Sep 22/;' input.log

答案 1 :(得分:0)

你去了:

#!/usr/bin/perl
use warnings;
use strict;
open (my $IN,'<','foo.log') or die "$!";
while (<$IN>) {
    last if /^Sep 22/;
    print join(':',/^(\w{3}\s\d{1,2})\s(\d{2}:\d{2}:\d{2}).+\((.+?)\) CMD \((.+?)\)/),"\n";
}
close $IN;

同样,单线风味:

perl -nle "exit if /^Sep 22/;print join(':',/^(\w{3}\s\d{1,2})\s(\d{2}:\d{2}:\d{2}).+\((.+?)\) CMD \((.+?)\)/)" foo.log

如果您希望以后能够再次运行脚本,可以使用当天:

#!/usr/bin/perl
use warnings;
use strict;
my (undef,undef,undef,$mday,$mon)=localtime(time);
my @abbr=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
open (my $IN,'<','foo.log') or die "$!";
while (<$IN>) {
    last if /^$abbr[$mon] $mday/;
    print join(':',/^(\w{3}\s\d{1,2})\s(\d{2}:\d{2}:\d{2}).+\((.+?)\) CMD \((.+?)\)/),"\n";
}
close $IN;
相关问题