-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtblcut.cc
201 lines (174 loc) · 4.29 KB
/
tblcut.cc
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* tblcut: fast 'cut' alternative - implementation
* Copyright(c) 2010 EURAC, Institute of Genetic Medicine
*/
/*
* Headers
*/
// local headers
#include "shared.hh"
// system headers
#include <map>
using std::multimap;
using std::make_pair;
#include <algorithm>
using std::find;
// c headers
#include <stdlib.h>
#include <unistd.h>
/*
* Types and constants
*/
typedef multimap<string, size_t> col_map;
/*
* Implementation
*/
void
help(char* argv[])
{
cerr << argv[0] << ": bad parameters:\n"
<< "Usage: " << argv[0] << " [-hd] < -f col,col,... | -n col,col,... > file\n"
<< "tblcut allows to extract single columns by name from the selected CSV file.\n"
<< "CSV files are TAB separated, containing column labels on the first row. You\n"
<< "can change the column separator by setting the TBLSEP environment variable.\n"
<< "\n"
<< " -d sep: set a different column separator\n"
<< " -f col,col,...: extract the selected column names\n"
<< " -n col,col,...: extract the selected column numbers\n"
<< " -c: complement the selected fields\n"
<< " -h: help summary\n";
}
int
main(int argc, char* argv[]) try
{
vector<string> fieldNames;
vector<size_t> fieldNums;
bool complement = false;
char sep = 0;
int arg;
while((arg = getopt(argc, argv, "hf:n:d:c")) != -1)
switch(arg)
{
case 'h':
help(argv);
return EXIT_SUCCESS;
case 'd':
sep = *optarg;
break;
case 'f':
tokenize(fieldNames, optarg, ",", true);
break;
case 'n':
{
vector<string> tmp;
tokenize(tmp, optarg, ",", true);
for(vector<string>::iterator it = tmp.begin(); it != tmp.end(); ++it)
fieldNums.push_back(strtoul(it->c_str(), NULL, 0));
}
break;
case 'c':
complement = !complement;
break;
default:
return EXIT_FAILURE;
}
// check args
argc -= optind;
const char* file(argv[optind++]);
if(argc != 1
|| (fieldNames.size() && fieldNums.size())
|| (!fieldNames.size() && !fieldNums.size()))
{
help(argv);
return EXIT_FAILURE;
}
// get default separator
if(!sep)
{
const char *envSep = getenv("TBLSEP");
if(envSep && *envSep) sep = *envSep;
else sep = '\t';
}
// open the file
const char* addr;
fix_string_matrix& m = *mapFixStringMatrix(&addr, file, sep);
// build the resulting column list
vector<size_t> cols;
if(fieldNums.size())
{
// check arguments
for(vector<size_t>::const_iterator it = fieldNums.begin();
it != fieldNums.end(); ++it)
{
if(!*it || *it > m.front().size())
{
cerr << file << ": invalid column number " << *it << "\n";
return EXIT_FAILURE;
}
}
// from column numbers
if(!complement)
{
for(vector<size_t>::const_iterator it = fieldNums.begin();
it != fieldNums.end(); ++it)
cols.push_back(*it - 1);
}
else
{
for(size_t i = 0; i != m.front().size(); ++i)
{
if(find(fieldNums.begin(), fieldNums.end(), i + 1) == fieldNums.end())
cols.push_back(i);
}
}
}
else
{
// from column names
if(!complement)
{
col_map cmap;
for(size_t i = 0; i != m.front().size(); ++i)
{
const string cname = m.front()[i];
cmap.insert(make_pair(string(m.front()[i]), i));
}
for(vector<string>::const_iterator it = fieldNames.begin();
it != fieldNames.end(); ++it)
{
col_map::const_iterator cIt = cmap.lower_bound(*it);
if(cIt == cmap.end())
{
cerr << file << ": unknown column \"" << *it << "\"\n";
return EXIT_FAILURE;
}
for(col_map::const_iterator cEnd = cmap.upper_bound(*it);
cIt != cEnd; ++cIt)
cols.push_back(cIt->second);
}
}
else
{
for(vector<fix_string>::const_iterator it = m.front().begin();
it != m.front().end(); ++it)
{
if(find(fieldNames.begin(), fieldNames.end(), string(*it)) == fieldNames.end())
cols.push_back(it - m.front().begin());
}
}
}
// output
for(fix_string_matrix::const_iterator it = m.begin(); it != m.end(); ++it)
{
vector<size_t>::const_iterator it2 = cols.begin();
cout << (*it)[*it2];
for(++it2; it2 != cols.end(); ++it2)
cout << sep << (*it)[*it2];
cout << '\n';
}
}
catch(runtime_error& e)
{
cerr << e.what() << std::endl;
return EXIT_FAILURE;
}