Pattern from a log within last hour

I want to extract a pattern from a log file within last hour.

I am doing

grep "pattern" filename | tail -1

It gives the last latest pattern but not within last hour.

and if the pattern found in last hour, output success or else failure.

We need the format of the log - a sample will be good.

1 Like
[8/5/13 9:50:45:456 EDT] CBWIJO1: error occured while connecting to server.
[8/5/13 9:50:46:123 EDT] CBWIJ15: check for database connection.

this is just a sample.
I need to find the for pattern "error" within last hour. and if it found echo found else not found.

This may need some tweak to work for you

# Getting the data for last hour
data_1h=$(awk '$0>=from && $0<=to' from="$(date +"["%D" "%H:%M%S -d -1hour)" to="$(date +"["%D" "%H:%M%S )" logfile)
# finding error and print
echo "$data_1h" | grep "error"
1 Like