Python 3.4 - Capture block of text based on single string -


i have searched far , wide , hope can either point me link missed or me out logic.

we have script goes out , collects logs various devices , places them in text files. within these text files there time stamp , need collect few lines of text before , after time stamp.

i have script matches time stamps , removes them reports (included below) cannot figure out how match time stamp , capture surrounding lines.

regex_time_stamp = re.compile('\d{2}:\d{2}:\d{2}|\d{1,2}y\d{1,2}w\d{1,2}d|\d{1,2}w\d{1,2}d|\d{1,2}d\d{1,2}h') open(filename, 'r') f:             h = f.readlines()             line in h:                 if regex_time_stamp.search(line) not none:                     new_line = re.sub(regex_time_stamp, '', line)                     pre_list.append(new_line)                 else:                     pre_list.append(line) 

any assistance appreciated! taking time read this.

the basic algorithm remember 3 read lines. when match header, read next 2 lines , combine header , last 3 lines you've saved.

alternately, since you're saving of lines in list, keep track of element current element, , when find header can go , previous 2 , next 2 elements.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -