Tags

, ,

Logfiles in older unix systems and monitoring systems just log the unix timestamp as the first field in the enteries. When we try to pull out data for a particular date, then it becomes tricky. The following code snippet will help pulling out data for a particular date quite easily

cat <logfile> | perl -lane ‘$_=$F[0]; s/\[//g ;s/\]//g; $date=`date -r $_` ; chomp($date); $out=$date . ” ” ; foreach(@F[1..scalar(@F)]) { chomp($_); $out=$out. $_.” “; }; print $out’

After the perl snippet you can use commands like grep to filter out data based on date.

alexj:~> cat sample.log

[1173070775] ns__stats<207.126.228.89>: 1.2.14
[1173070832] ns__exec<207.126.228.89>: check_mysql_variable!-p=var!-u=check_var: code=0

alexj:~> cat sample.log | perl -lane ‘$_=$F[0]; s/\[//g ;s/\]//g; $date=`date -r $_` ; chomp($date); $out=$date . ” ” ; foreach(@F[1..scalar(@F)]) { chomp($_); $out=$out. $_.” “; }; print $out’

Sun Mar 4 20:59:35 PST 2007 ns__stats<207.126.228.89>: 1.2.14
Sun Mar 4 21:00:32 PST 2007 ns__exec<207.126.228.89>: check_mysql!-p=xx!-u=xx: code=0

Some alternate way to do it :

cat file | perl -p -e ‘s/^\[(\d+)\] /localtime($1)/e;’ //source philip

Code snippet just to pre append time to every line :

perl -p ‘print time, ” “;’ //thanx philp

Advertisements