forked from jgurtowski/ectools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.py
44 lines (32 loc) · 841 Bytes
/
filter.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
##
#Filters file's first column by first column of db
# -dbentry flag outputs the entry in the database instead of the
# query
##
import sys
import os
if len(sys.argv) < 3:
print "filter.py db file.txt [-dbentry]"
sys.exit(1)
db = {}
dbentry = False
if sys.argv > 3:
if "-dbentry" in sys.argv[3:]:
dbentry = True
(dbfile,infile) = sys.argv[1:3]
if not os.path.exists(infile):
sys.exit("Can't find %s" % infile)
sys.stderr.write("Loading db...")
with open(dbfile) as dbfh:
for line in dbfh:
db[line.strip().split()[0]] = line
sys.stderr.write("done\n")
with open(infile) as infh:
for line in infh:
k = line.strip().split()[0]
l = line
if db.has_key(k):
if dbentry:
l = db[k]
sys.stdout.write(l)