July 03, 2008
grep | sed
以下のような実行ログから失敗したid(\[\d+\]の部分)を抽出する。
結果をfailures.phpに出力。
使用する側のプログラムではissetでチェックしたいのでこの形式に。
in_array()だと遅い。
これに少しはまった。
20080701 15:20:01 SUCCESS: username [10502] 20080701 18:14:41 FAILURE: username [3821] 20080702 08:39:22 SUCCESS: username [9134] 20080702 14:43:52 FAILURE: username [6810] 20080702 21:11:05 SUCCESS: username [8819] 20080703 08:31:34 SUCCESS: username [13266] ...grepで失敗の行を取り出してsedに渡す。
結果をfailures.phpに出力。
grep FAILURE some.log | sed -e 's/^.*\[\([0-9]*\)\]/\1 => 1,/' > failures.phpこんな感じになる。
3821 => 1, 6810 => 1, ...ちょっと付け足してphpの配列にする。
使用する側のプログラムではissetでチェックしたいのでこの形式に。
in_array()だと遅い。
<?php
$failures = array(
3821 => 1,
6810 => 1,
...
);
...
if (!isset($failures[$targetId])) {
// process
}
キャプチャに使う括弧をバックスラッシュでエスケープする必要がある。これに少しはまった。