-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.py
More file actions
31 lines (27 loc) · 1.06 KB
/
filter.py
File metadata and controls
31 lines (27 loc) · 1.06 KB
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
31
#! /usr/bin/env python3
import argparse
import sys
def filter(fil, inp, out, delim="\t"):
fil = list(fil)
for line in inp:
for f in fil:
f = f.split(delim)
for i in f:
i = i.strip()
if line.lower().find(i.lower()) < 0:
break
else:
break # all elements of f matched
else:
continue # no element of fil matched
print(line, file=out, end="") # line already contains a \n
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--filter", required=True, type=argparse.FileType('r'))
parser.add_argument("input", type=argparse.FileType('r'), default=sys.stdin)
parser.add_argument("-o", "--output", nargs='?', type=argparse.FileType('w'), default=sys.stdout)
parser.add_argument("-d", "--delim", help="Delimiter to use for separating the elements in filer", default="\t")
args = parser.parse_args()
filter(args.filter, args.input, args.output, delim=args.delim)
if __name__ == "__main__":
main()