[memo][perl][linux] ファイルから最近アペンドされた n行を取得する
ログファイルのようにアペンド保存されているファイルから,最近アペンドされた n行を取得する一方法のメモ.個人的に今まで気づかなかったので.
my $n = <取得数>;
my $file = <対象ファイル名>;
my @recent_appended = reverse split( m{\x0d\x0a?|\x0a}, `tail -$n $file` );
tail コマンドを使えば簡単ですね.
次のようにすれば head コマンドや tailコマンドのように使えそうです.
#!/usr/bin/perl
use strict;
use warnings;
$ARGV[0] =~ s{-}{}; # 取得数に ‘-’ をつけてもつけなくてもいいように...
print join( "\n", reverse split( m{\x0d\x0a?|\x0a}, `tail -$ARGV[0] $ARGV[1]` ) ) . "n";




![twitomonitor [ついともにた] twitomonitor [ついともにた]](http://www.iwa-ya.net/img/banner/twitomonitor.png)


