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

Perl 如何将日志文件打印到特定日期,perl,scripting,Perl,Scripting,我有cron日志文件的格式 当前日期为9月22日 9月22日13:00:01主机名crond[24359]:(root)CMD(/usr/lib64/sa/sa1) 我想用以下格式打印日志文件,直到“9月21日” date:time:user:command format 那么: perl -pe 'exit if /^Sep 22/;' input.log 好了: #!/usr/bin/perl use warnings; use strict; open (my $IN,'<','

我有cron日志文件的格式 当前日期为9月22日

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

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

date:time:user:command format
那么:

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

#!/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;
如果您希望将来能够再次运行脚本,可以使用当前日期:

#!/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;
#/usr/bin/perl
使用警告;
严格使用;
my(未定义,未定义,未定义,$mday,$mon)=本地时间(time);
my@abbr=qw(1-2-3-4-5-6-7-8-9-10-11-12);

打开(我的美元),你试过什么?
#!/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;