I am trying to watch a log for certain messages within the last hour. The logs are formatted in this manner:
[1/18/19 9:59:13:791 CST] <Extra text here...>
I was having trouble with just doing a date comparison with awk, so my thought was to convert to epoch and compare. I am taking field 1 and cutting off the milliseconds from field 2, and removing [] (though I guess I could just do [ for my purposes).
while read -r line || [[ -n "$line" ]]
do
log_date_str="$(awk '{gsub("\\[|\\]", "");print $1" "substr($2,1,length($2)-4)}' <<< "$line")"
log_date="$(date -d "$log_date_str" +%s)"
[[ $(($(date +%s)-$log_date)) -le 3600 ]] && echo "$line"
done < /path/to/file
When I try to run this against the log file though, I get this error:
date: invalid date `************ S'
-bash: 1547832909-: syntax error: operand expected (error token is "-")
Taking a single date, e.g. "1/18/19 9:59:13" works with the date conversion to epoch, but I'm not sure where to go with that error.
bash -x yourscriptwill let you see the commands as they're actually run, with the variables' real-world values; that makes it a lot easier to isolate what's going on, especially here where we don't know what yourlinecontains, or whatlog_date_strorlog_datevalues are in practice.(( ( $(date +%s) - log_date ) <= 3600 )) && echo "$line"is less syntax.************ Sinstead of the date and time. You need to filter out lines that don't follow the expected format.