包含最近1年数据的滚动档案文件

时间:2015-01-16 12:35:39

标签: perl shell archive

需要开发一个脚本来创建仅包含最近1年数据的存档文件。脚本将复制主数据文件(CEMI.log)的内容,并将其附加到存档日志文件(CEMI.Archive.log)。这将在一周内发生一次(每个星期一凌晨3点)。一年后,脚本应该能够从存档文件中删除超过一年的数据。

我陷入了需要删除最近一年数据的地步。如何使用shell或perl脚本完成此操作?

示例文件:

-bash-3.2# more test.txt
2015-01-15,09:17:10,101,20a6475d-4d0c-4fe4-8765-35065ddfe887,_1.1,L,
2015-01-15,09:18:57,70,al Test,20a6475d-4d0c-4fe4-8765-35065ddfe887,1,L,
2015-01-15,10:59:28,1,,,,Best Practice,9f02745244d6440584b24012d882f935,,L,
2015-01-15,11:00:52,2,,,,Best Practice,9f02745244d6440584b24012d882f935,,L,
2015-01-15,11:00:56,1,,,,Best Practice,9f02745244d6440584b24012d882f935,,L,
2015-01-15,11:03:14,1,,,,Best Practice,9f02745244d6440584b24012d882f935,,L,
2015-01-15,11:03:38,1,,,,Best Practice,9f02745244d6440584b24012d882f935,,L,
2015-01-15,11:04:01,1,,,,Best Practice,9f02745244d6440584b24012d882f935,,L,
2015-01-15,11:05:07,2,,,,Best Practice,9f02745244d6440584b24012d882f935,,L,
2015-01-15,11:06:45,32,,,,Best Practice,9f02745244d6440584b24012d882f935,,L,
2015-01-15,12:57:13,36,,,560909,Best Practice,e8418950-6561-4465-b16b-30e118e826b7,,L,
2015-01-15,13:37:56,1032,,xml-data1,Test,20a6475d-4d0c-4fe4-8765-35065ddfe887,Request_1.1,L,Success
2015-01-15,13:39:01,38,,,Internal Test,20a6475d-4d0c-4fe4-8765-35065ddfe887,Request_1.1,L,
2015-01-15,13:39:50,113,,xml-dat1,al Test,20a6475d-4d0c-4fe4-8765-35065ddfe887,Request_1.1,L,
2015-01-15,13:40:17,74,,, Test,20a6475d-4d0c-4fe4-8765-35065ddfe887,Request_1.1,L,

1 个答案:

答案 0 :(得分:0)

这将在第一行停止,与您的日期格式不匹配。

#!/usr/bin/env perl

use strict;
use warnings;

use DateTime;
use File::Copy qw/move/;
use File::Temp qw/tempfile/;

# filenames
my $log = "test.txt";
my ( undef, $tmp ) = tempfile( undef, OPEN => 0 ); # get safe temporary filename, but do not open file

# move old log file to temporary location
move $log => $tmp
  or die "Cannot rename '$log' to '$tmp': $!";

# open temporary file (contains old log) and new log
open( my $in, "<", $tmp )
  or die "Cannot open '$tmp': $!";

open( my $out, ">", $log )
  or die "Cannot open '$log': $!";

# calculate a DateTime value one year in the past
my $limit = DateTime->today->subtract( years => 1 );

# skip lines with date that is too old
while (<$in>) {
    if (m/^(\d\d\d\d)-(\d\d)-(\d\d)/) {
        # get DateTime object from matched date parts
        my $dt = DateTime->new(
            year  => $1,
            month => $2,
            day   => $3,
        );

        # keep on skipping lines while dates are too old
        if ( $dt < $limit ) {
            next;
        }
    }

    # if no date was found or date too young, end skipping
    last;
}

# copy all remaining lines from temporary file to new log
while (<$in>) {
    print $out $_;
}

close $in  or warn "Error closing '$in': $!";
close $out or warn "Error closing '$out': $!";

unlink $tmp or die "Cannot delete '$tmp': $!";

如何执行此操作:

$ cat > test.txt # press Ctrl+D to end text input
2014-01-01,will be removed
stops here
2014-01-01,will be kept because stopped
2015-01-01,would be kept anyway
$ perl rotate_log.pl 
$ cat test.txt 
2014-01-01,will be kept because stopped
2015-01-01,would be kept anyway