forked from dathere/qsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.rs
92 lines (73 loc) · 2.81 KB
/
select.rs
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
static USAGE: &str = r#"
Select columns from CSV data efficiently.
This command lets you manipulate the columns in CSV data. You can re-order
them, duplicate them or drop them. Columns can be referenced by index or by
name if there is a header row (duplicate column names can be disambiguated with
more indexing). Column ranges can also be specified. Finally, columns can be
selected using regular expressions.
Select the first and fourth columns:
$ qsv select 1,4
Select the first 4 columns (by index and by name):
$ qsv select 1-4
$ qsv select Header1-Header4
Ignore the first 2 columns (by range and by omission):
$ qsv select 3-
$ qsv select '!1-2'
Select the third column named 'Foo':
$ qsv select 'Foo[2]'
Select columns using a regex using '/<regex>/':
$ qsv select /^a/
$ qsv select '/^.*\d.*$/'
# remove SSN, account_no and password columns
$ qsv select '!/SSN|account_no|password/'
Re-order and duplicate columns arbitrarily:
$ qsv select 3-1,Header3-Header1,Header1,Foo[2],Header1
Quote column names that conflict with selector syntax:
$ qsv select '\"Date - Opening\",\"Date - Actual Closing\"'
For more examples, see https://github.com/jqnatividad/qsv/blob/master/tests/test_select.rs.
Usage:
qsv select [options] [--] <selection> [<input>]
qsv select --help
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
-n, --no-headers When set, the first row will not be interpreted
as headers. (i.e., They are not searched, analyzed,
sliced, etc.)
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
"#;
use serde::Deserialize;
use crate::{
config::{Config, Delimiter},
select::SelectColumns,
util, CliResult,
};
#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
arg_selection: SelectColumns,
flag_output: Option<String>,
flag_no_headers: bool,
flag_delimiter: Option<Delimiter>,
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let rconfig = Config::new(&args.arg_input)
.delimiter(args.flag_delimiter)
.no_headers(args.flag_no_headers)
.select(args.arg_selection);
let mut rdr = rconfig.reader()?;
let mut wtr = Config::new(&args.flag_output).writer()?;
let headers = rdr.byte_headers()?.clone();
let sel = rconfig.selection(&headers)?;
if !rconfig.no_headers {
wtr.write_record(sel.iter().map(|&i| &headers[i]))?;
}
let mut record = csv::ByteRecord::new();
while rdr.read_byte_record(&mut record)? {
wtr.write_record(sel.iter().map(|&i| &record[i]))?;
}
wtr.flush()?;
Ok(())
}