Working to print lines that match a specific pattern between a start time and an end time.
Example log line:
Fri Mar 01 HH:MM:SS YYYY:thresholdcrossed
I am looking for an equivalent of the following:
cat /path/file.log | egrep -v "exec|param" | grep thresholdcrossed | sed -n /$start/,/$stop/ >output.file
The start and stop will be user generated variables based on the start and stop times they are searching.
This will give me matches for a single line using regular expressions, but I can't get all lines that match between 2 time stamps.
for line in hand:
line = line.rstrip()
if re.search("Mar 10 21.+:.+tstat-threshcrosso", line) :
print line
# user defined time range in milliseconds since epoch and
start_ms =
end_ms =
# user defined pattern
pattern = "tstat-threshcrosso"
# loop through whole file
for line in hand:
# strip the line
line = line.rstrip()
# parse the line to get time_stamp_ms (ms since epoch) and log_message
time_stamp_ms =
log_message =
# break if we went beyond end time
if time_stamp_ms > end:
break
# check if the time stamp is within user defined period
if time_stamp_ms < start_ms:
continue
# check for string pattern match
if pattern in log_message:
print line
You can optimize this by doing a binary search to find the first line after the start time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.