forked from etsy/statsd-jvm-profiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filterlines.py
30 lines (26 loc) · 872 Bytes
/
filterlines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
from optparse import OptionParser
import sys
parser = OptionParser()
parser.add_option('-f', '--filter', dest='filter', help='Filter for strings (list of strings which WON''T go into the output)', metavar='FILTER')
args, inputfile = parser.parse_args()
if len(inputfile) > 1:
print "Format: filterlines.py [-f filter.txt] [inputfile.txt]"
sys.exit(255)
# build the filtering set
filter_exclude = set()
if args.filter:
with open(args.filter) as f:
for s in f:
filter_exclude.add(s.rstrip())
# read the file and output filtered lines
if len(inputfile) == 1:
sys.stdin = open(inputfile[0], "r")
for line in sys.stdin.readlines():
found = False
for filter_string in filter_exclude:
if filter_string in line:
found = True
break
if not found:
print(line.rstrip())