-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselect_row.py
More file actions
executable file
·49 lines (38 loc) · 1.17 KB
/
select_row.py
File metadata and controls
executable file
·49 lines (38 loc) · 1.17 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python
# select a set of rows based on input file
# usage:
# select_row.py <selectors: each row as a line> <input_file>
import sys
queryFILE = sys.argv[1]
targetFILE = sys.argv[2]
# handle input from pipe
if queryFILE == '-':
f = sys.stdin
else:
f = open(queryFILE, 'rU')
# build a dict of queries
query_dict = {}
for l in f:
# skip the lines masked as command, may be header
if not l.startswith('#'):
query_dict[l.strip()] = 1
f.close()
target_dict = {}
with open(targetFILE) as inH:
#header = inH.readline().strip()
#print header
for line in inH:
fields = line.strip().split()
if fields[0] in query_dict:
print(line.strip())
query_dict[fields[0]]=2
not_in_list = []
for i in query_dict:
if query_dict[i] == 1:
not_in_list += [i]
if not_in_list:
sys.stderr.write("=======================================================\n")
sys.stderr.write("Queries cannot be found in the file:\n")
sys.stderr.write('\t'.join(not_in_list)+'\n')
sys.stderr.write("=======================================================\n")
sys.stderr.write("\n\n\n")