John Criswell | 21eccb6 | 2004-08-02 22:28:50 +0000 | [diff] [blame] | 1 | # |
2 | # Usage: | ||||
3 | # prcontext <pattern> <# lines of context> | ||||
4 | # | ||||
5 | |||||
6 | import sys | ||||
7 | |||||
8 | # | ||||
9 | # Get the arguments | ||||
10 | # | ||||
11 | pattern=sys.argv[1] | ||||
12 | num=int(sys.argv[2]) | ||||
13 | |||||
14 | # | ||||
15 | # Get all of the lines in the file. | ||||
16 | # | ||||
17 | lines=sys.stdin.readlines() | ||||
18 | |||||
19 | index=0 | ||||
20 | for line in lines: | ||||
21 | if ((line.find(pattern)) != -1): | ||||
22 | if (index-num < 0): | ||||
23 | bottom=0 | ||||
24 | else: | ||||
25 | bottom=index-num | ||||
26 | for output in lines[bottom:index+num+1]: | ||||
27 | print output[:-1] | ||||
28 | index=index+1 | ||||
29 |