I'm writing a ksh script to parse a log file and send an email when important messages are found. Some messages are informational and I'd like to ignore them.
The log file has format
2018-01-24.08.24.35.875675 some text more text more text more text more text 2018-01-24.08.24.37.164538 some text more text more text INF9999W <-- informational text more text 2018-01-24.08.24.46.8602545 some text more text more text more text
The timestamp would be considered the message separator, with the timestamp belonging to the message that follows it. I'd like to search the file for each occurrences of "information text" and then remove that whole message from the file (from preceding timestamp until just before the next timestamp).
How can I easily determine the line numbers of the preceding and following timestamps so I remove those lines with:
awk 'NR<'$preceding_ts' || NR >='$following_ts'
My way is to get all the timestamp lines into a file and then loop through that file until I find the timestamp lines that are just before and after the 'information-text' line #. It seems like a lot of work, particularly on a large file. Is there a more efficient way.
integer inf_line integer last_ts_line integer cur_ts cp $error_log $copy_log while true do inf_line=$(grep -n "INF99999W" $copy_log | head -1 | cut -f1 -d":") if [[ $inf_line -eq 0 ]] then break fi grep -n -E "^20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]-" $copy_log | cut -f1 -d":" > $ts_lines last_ts_line=99999999 cat $ts_lines | while read cur_ts do if [[ $cur_ts -gt $inf_line && $last_ts_line -lt $inf_line ]] then awk 'NR<'$last_ts_line' || NR >='$cur_ts'' $copy_log > $temp_log cp $temp_log $copy_log last_ts_line=$cur_ts break fi last_ts_line=$cur_ts done if [[ $last_ts_line -lt $inf_line ]] then awk 'NR<'$last_ts_line'' $copy_log > $temp_log cp $temp_log $copy_log fi done