0

log.txt ファイルにログ ファイルがあり、ユーザー名を最新のログインで並べ替えたい

例 - ユーザー PAUL が 1 月に 2 回ログインし、2 月に 1 回ログインした場合、2 月のログインの詳細を取得する必要があります (PAUL の最後のログインとして)。

入力ファイル: log.txt--

Administrator-25/02/2013
Administrator-26/03/2013

出力ファイル-

Administrator-26/03/2013
4

3 に答える 3

0

Unix 時間を使用した日付の比較:

#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(mktime);
$\="\n";

open my $fh, '<','log.txt' or die $!;
my %h1;
while(<$fh>){
        chomp;
        my ($user,$date,$mon,$year)=split(/[\/-]/);  #Split record 
        my $tm=mktime(0,0,0,$date,$mon-1,$year-1900); # Calculate the epoch
        if (!exists$h1{$user} or ($h1{$user}<$tm)){  # store the bigger value in hash with the use as the key
                $h1{$user}=$tm;
        }
}
# Sort the hash on the basis of the key and print it
foreach my $k (sort keys%h1){
        my @val=localtime($h1{$k});

        $val[4]+=1;
        $val[5]+=1900;
        print $k,'-',join "/",@val[3..5];
}

入力ファイル (log.txt):

Administrator-25/02/2013
Administrator-26/03/2013
Administrator-27/02/2013
xyz-31/01/2013
xyz-31/03/2013

スクリプトの実行時:

xyz-31/3/2013
Administrator-26/3/2013
于 2013-04-29T08:50:31.613 に答える