Skip to main content
3 of 5
deleted 154 characters in body; edited tags
terdon
  • 252.3k
  • 69
  • 480
  • 718

Break up large log files

I am trying to break a large log file into smaller files based on date.

The file is of the following form, where some lines may not have a date. Those lines should be included with the previous dated line.

2014-04-07 23:59:58 CheckForCallAction [ERROR] Exception caught
Undated line 1
Undated line 2
2014-04-08 00:00:03 MobileAppRequestFilter [DEBUG] Action
undated line 3
2015-04-08 00:00:03 MobileAppRequestFilter [DEBUG] ActionB

I found How to extract logs between two time stamps which is close to what I want, except my log file does not include a "[" at the start of the date, or "]" and the end of the date.

The command from that link is:

awk -F'[[]|[]]' \
  '$0 ~ /^\[/ && $2 >= "2014-04-07 23:00" { p=1 }
   $0 ~ /^\[/ && $2 >= "2014-04-08 02:00" { p=0 }
   p { print $0 }' > test1.log  logwith[.log

I have been trying for several days to modify this, but I just can't seem to get it.

A desired enhancement would be to not have to specify a start and end date, but rather automatically name the output files by either year, or year-month.

Mike
  • 65
  • 1
  • 9