Bash: Log-splitting

Ever had an access log that grew way out of bounds because it wasn't rotated properly? Well, most sys-admins have, and trust me it is frustrating! A quick way to get through those pesky apache/nginx files is by using AWK. It is a very fast way at that as well plus it doesn't require any loops either! It makes splitting logs by date extremely easy!


awk -F"[]/:[]" '
BEGIN {
  m2n["Jan"] =  1;  m2n["Feb"] =  2; m2n["Mar"] =  3; m2n["Apr"] =  4;
  m2n["May"] =  5;  m2n["Jun"] =  6; m2n["Jul"] =  7; m2n["Aug"] =  8;
  m2n["Sep"] =  9;  m2n["Oct"] = 10; m2n["Nov"] = 11; m2n["Dec"] = 12;
}
{
  if($4 != pyear || $3 != pmonth || $2 != pday) {
    pyear  = $4
    pmonth = $3
    pday   = $2

    if(fname != "")
      close(fname)

    fname  = sprintf("access_%04d_%02d_%02d.log", $4, m2n[$3], $2)
  }
  print > fname
}' access.log
Author: Angelique Dawnbringer Published: 2016-12-18 03:09:55 Keywords:
  • Logs
  • Access
  • Apache
  • Bash
Modified: 2017-09-10 17:53:38