grep_range.py

grep_range.py is for extracting a partial section of a log file. For example, if you were only interested in records between two time period, you might do this:

binpython grep_range.py 2010-10-28T23:46:52 2010-10-28T23:47:54 event.log

Here’s the source

#!python
import sys
start_expr = sys.argv[1]
end_expr   = sys.argv[2]
filename   = sys.argv[3]

fd = open(filename, 'r')

record = False
find_expr = start_expr
for line in fd.readlines():
  if line.find(find_expr) >= 0:
     if find_expr == start_expr:
        find_expr = end_expr
        record = True
     else:
        break
  if record:
     sys.stdout.write(line)

About this entry