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-07T23:59:58 CheckForCallAction [ERROR] Exception caught
Undated line 1
Undated line 2
2014-04-08T00:00:03 MobileAppRequestFilter [DEBUG] Action
undated line 3
2015-04-08T00: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.
cutcommand or$1inawkreplaces the actual date and$2the actual time. In short, you don't need square brackets. The post you linked is a common case but not necessarily the norm.