1

exim4 MTA 用のログ パーサーを作成するつもりですが、いくつか質問があります。(exilogプログラムがあることは知っています)

質問: 1.行を解析するより良い方法は何ですか? (そのような行の約 5Gb :D ) ive はこの $line を取得しました:

2011-12-24 12:32:12 MeSSag3-Id-Ye <hostname> (from@some.email) <to@some.email> => H=[321.123.321.123] T="Hello this is a test"

このフィールドをすべて変数に取得したい。私は今そのようなものを使用しています($var,[var2])=($line =~ /somecoolregexp/ )。それは速い/良いですか、それとも他のものを使用する必要がありますか?

4

1 に答える 1

1

まあ、それはあなたがデータで何をしたいかによって異なります。

これに大きな問題があると仮定すると、while (<>) { ... }分割を使用するだけで最も簡単な解析を取得できます。

my @fields = split;

次のレベルは、少し意味を追加することです

my ($date, $time, $id, $host, $from, $to, undef, $dest) = split;

undef(注:結果を無視する場合は、に割り当てることができます)

最後に、正規表現を使用することで、多くの不要な作業をクリーンアップできます。上記の分割を小さな正規表現と組み合わせて、各フィールドを個別にクリーニングすることもできます。

my ($datetime, $id, $host, $from, $to, $dest) = 
    /([\d-]+ [\d:]+) \s+     # date and time together
     (\S+)           \s+     # message id, just a block of non-whitespace
     <(.*?)>         \s+     # hostname in angle brackets, .*? is non-greedy slurp
    \((.*?)\)        \s+     # from email in parens
     <(.*?)>         \s+     # to email in angle brackets
      \S+            \s+     # separated between to-email and dest
      (\S+)                  # last bit, could be improved to (\w)=\[(.*?)\]
     /x;                     # /x lets us break all of this up, so its a bit readable

もちろん、これをあらゆる種類のばかげたことに取り続けることができますが、これらのフィールドのより具体的な解析を開始する場合は、最初の分割に続いて分割フィールドの解析を行います。例えば:

 my ($date, $time, ...) = split;

 my ($year, $month, $day)    = split(/-/, $date);
 my ($hour, $min,   $sec)    = split(/:/, $time);
 my ($from_user, $from_host) = ( $from =~ /< ([^\@]+) \@ (.*) >/x );
 ...etc...
于 2011-04-21T07:17:55.060 に答える