csvutils 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +3 -3
- data/LICENSE.md +116 -0
- data/Manifest.txt +8 -4
- data/README.md +302 -286
- data/Rakefile +30 -26
- data/bin/csvcut +17 -17
- data/bin/csvhead +17 -17
- data/bin/csvheader +17 -17
- data/bin/csvsplit +17 -17
- data/bin/csvstat +17 -17
- data/{test/data → datasets}/at-austria/AUT.csv +363 -363
- data/{test/data → datasets}/de-deutschland/bundesliga.csv +481 -481
- data/{test/data → datasets}/eng-england/2017-18/E0.csv +381 -381
- data/lib/csvutils.rb +32 -31
- data/lib/csvutils/commands/cut.rb +43 -43
- data/lib/csvutils/commands/head.rb +40 -40
- data/lib/csvutils/commands/header.rb +35 -35
- data/lib/csvutils/commands/split.rb +41 -41
- data/lib/csvutils/commands/stat.rb +41 -41
- data/lib/csvutils/cut.rb +43 -50
- data/lib/csvutils/head.rb +22 -25
- data/lib/csvutils/header.rb +16 -28
- data/lib/csvutils/split.rb +106 -107
- data/lib/csvutils/stat.rb +81 -86
- data/lib/csvutils/test.rb +19 -22
- data/lib/csvutils/utils.rb +29 -13
- data/lib/csvutils/version.rb +24 -24
- data/test/helper.rb +16 -16
- data/test/test_cut.rb +31 -0
- data/test/test_head.rb +30 -0
- data/test/{test_headers.rb → test_header.rb} +50 -50
- data/test/test_misc.rb +44 -44
- data/test/test_split.rb +31 -0
- data/test/test_version.rb +20 -20
- metadata +28 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0be624b7103c77a6542c61522d097a807e207204
|
4
|
+
data.tar.gz: 4d6dffcadc2d6906e59eb009085a4b0cf822b81d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 839586401ebf58210d448cfe42f1d3959abbc640f93540e7127dad6fd04666c6cd3f31f07b4cf659000c2e5deda0db32fdbdf2d59d55beb87533ad24f275e193
|
7
|
+
data.tar.gz: 69219118ab9871d44797078da35ef5c060b50399334ea00c219471658ce07045f55ab76213ab6743e358e04e1f0be2c4fcf3036274665bc870ee4067530c9cb5
|
data/HISTORY.md
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
### 0.0.1 / 2018-08-04
|
2
|
-
|
3
|
-
* Everything is new. First release.
|
1
|
+
### 0.0.1 / 2018-08-04
|
2
|
+
|
3
|
+
* Everything is new. First release.
|
data/LICENSE.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
CC0 1.0 Universal
|
2
|
+
|
3
|
+
Statement of Purpose
|
4
|
+
|
5
|
+
The laws of most jurisdictions throughout the world automatically confer
|
6
|
+
exclusive Copyright and Related Rights (defined below) upon the creator and
|
7
|
+
subsequent owner(s) (each and all, an "owner") of an original work of
|
8
|
+
authorship and/or a database (each, a "Work").
|
9
|
+
|
10
|
+
Certain owners wish to permanently relinquish those rights to a Work for the
|
11
|
+
purpose of contributing to a commons of creative, cultural and scientific
|
12
|
+
works ("Commons") that the public can reliably and without fear of later
|
13
|
+
claims of infringement build upon, modify, incorporate in other works, reuse
|
14
|
+
and redistribute as freely as possible in any form whatsoever and for any
|
15
|
+
purposes, including without limitation commercial purposes. These owners may
|
16
|
+
contribute to the Commons to promote the ideal of a free culture and the
|
17
|
+
further production of creative, cultural and scientific works, or to gain
|
18
|
+
reputation or greater distribution for their Work in part through the use and
|
19
|
+
efforts of others.
|
20
|
+
|
21
|
+
For these and/or other purposes and motivations, and without any expectation
|
22
|
+
of additional consideration or compensation, the person associating CC0 with a
|
23
|
+
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
|
24
|
+
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
|
25
|
+
and publicly distribute the Work under its terms, with knowledge of his or her
|
26
|
+
Copyright and Related Rights in the Work and the meaning and intended legal
|
27
|
+
effect of CC0 on those rights.
|
28
|
+
|
29
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
30
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
31
|
+
Related Rights"). Copyright and Related Rights include, but are not limited
|
32
|
+
to, the following:
|
33
|
+
|
34
|
+
i. the right to reproduce, adapt, distribute, perform, display, communicate,
|
35
|
+
and translate a Work;
|
36
|
+
|
37
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
38
|
+
|
39
|
+
iii. publicity and privacy rights pertaining to a person's image or likeness
|
40
|
+
depicted in a Work;
|
41
|
+
|
42
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
43
|
+
subject to the limitations in paragraph 4(a), below;
|
44
|
+
|
45
|
+
v. rights protecting the extraction, dissemination, use and reuse of data in
|
46
|
+
a Work;
|
47
|
+
|
48
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
49
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
50
|
+
protection of databases, and under any national implementation thereof,
|
51
|
+
including any amended or successor version of such directive); and
|
52
|
+
|
53
|
+
vii. other similar, equivalent or corresponding rights throughout the world
|
54
|
+
based on applicable law or treaty, and any national implementations thereof.
|
55
|
+
|
56
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention of,
|
57
|
+
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
|
58
|
+
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
|
59
|
+
and Related Rights and associated claims and causes of action, whether now
|
60
|
+
known or unknown (including existing as well as future claims and causes of
|
61
|
+
action), in the Work (i) in all territories worldwide, (ii) for the maximum
|
62
|
+
duration provided by applicable law or treaty (including future time
|
63
|
+
extensions), (iii) in any current or future medium and for any number of
|
64
|
+
copies, and (iv) for any purpose whatsoever, including without limitation
|
65
|
+
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
|
66
|
+
the Waiver for the benefit of each member of the public at large and to the
|
67
|
+
detriment of Affirmer's heirs and successors, fully intending that such Waiver
|
68
|
+
shall not be subject to revocation, rescission, cancellation, termination, or
|
69
|
+
any other legal or equitable action to disrupt the quiet enjoyment of the Work
|
70
|
+
by the public as contemplated by Affirmer's express Statement of Purpose.
|
71
|
+
|
72
|
+
3. Public License Fallback. Should any part of the Waiver for any reason be
|
73
|
+
judged legally invalid or ineffective under applicable law, then the Waiver
|
74
|
+
shall be preserved to the maximum extent permitted taking into account
|
75
|
+
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
|
76
|
+
is so judged Affirmer hereby grants to each affected person a royalty-free,
|
77
|
+
non transferable, non sublicensable, non exclusive, irrevocable and
|
78
|
+
unconditional license to exercise Affirmer's Copyright and Related Rights in
|
79
|
+
the Work (i) in all territories worldwide, (ii) for the maximum duration
|
80
|
+
provided by applicable law or treaty (including future time extensions), (iii)
|
81
|
+
in any current or future medium and for any number of copies, and (iv) for any
|
82
|
+
purpose whatsoever, including without limitation commercial, advertising or
|
83
|
+
promotional purposes (the "License"). The License shall be deemed effective as
|
84
|
+
of the date CC0 was applied by Affirmer to the Work. Should any part of the
|
85
|
+
License for any reason be judged legally invalid or ineffective under
|
86
|
+
applicable law, such partial invalidity or ineffectiveness shall not
|
87
|
+
invalidate the remainder of the License, and in such case Affirmer hereby
|
88
|
+
affirms that he or she will not (i) exercise any of his or her remaining
|
89
|
+
Copyright and Related Rights in the Work or (ii) assert any associated claims
|
90
|
+
and causes of action with respect to the Work, in either case contrary to
|
91
|
+
Affirmer's express Statement of Purpose.
|
92
|
+
|
93
|
+
4. Limitations and Disclaimers.
|
94
|
+
|
95
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
96
|
+
surrendered, licensed or otherwise affected by this document.
|
97
|
+
|
98
|
+
b. Affirmer offers the Work as-is and makes no representations or warranties
|
99
|
+
of any kind concerning the Work, express, implied, statutory or otherwise,
|
100
|
+
including without limitation warranties of title, merchantability, fitness
|
101
|
+
for a particular purpose, non infringement, or the absence of latent or
|
102
|
+
other defects, accuracy, or the present or absence of errors, whether or not
|
103
|
+
discoverable, all to the greatest extent permissible under applicable law.
|
104
|
+
|
105
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
106
|
+
that may apply to the Work or any use thereof, including without limitation
|
107
|
+
any person's Copyright and Related Rights in the Work. Further, Affirmer
|
108
|
+
disclaims responsibility for obtaining any necessary consents, permissions
|
109
|
+
or other rights required for any use of the Work.
|
110
|
+
|
111
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
112
|
+
party to this document and has no duty or obligation with respect to this
|
113
|
+
CC0 or use of the Work.
|
114
|
+
|
115
|
+
For more information, please see
|
116
|
+
<http://creativecommons.org/publicdomain/zero/1.0/>
|
data/Manifest.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
HISTORY.md
|
2
|
+
LICENSE.md
|
2
3
|
Manifest.txt
|
3
4
|
README.md
|
4
5
|
Rakefile
|
@@ -7,6 +8,9 @@ bin/csvhead
|
|
7
8
|
bin/csvheader
|
8
9
|
bin/csvsplit
|
9
10
|
bin/csvstat
|
11
|
+
datasets/at-austria/AUT.csv
|
12
|
+
datasets/de-deutschland/bundesliga.csv
|
13
|
+
datasets/eng-england/2017-18/E0.csv
|
10
14
|
lib/csvutils.rb
|
11
15
|
lib/csvutils/commands/cut.rb
|
12
16
|
lib/csvutils/commands/head.rb
|
@@ -21,10 +25,10 @@ lib/csvutils/stat.rb
|
|
21
25
|
lib/csvutils/test.rb
|
22
26
|
lib/csvutils/utils.rb
|
23
27
|
lib/csvutils/version.rb
|
24
|
-
test/data/at-austria/AUT.csv
|
25
|
-
test/data/de-deutschland/bundesliga.csv
|
26
|
-
test/data/eng-england/2017-18/E0.csv
|
27
28
|
test/helper.rb
|
28
|
-
test/
|
29
|
+
test/test_cut.rb
|
30
|
+
test/test_head.rb
|
31
|
+
test/test_header.rb
|
29
32
|
test/test_misc.rb
|
33
|
+
test/test_split.rb
|
30
34
|
test/test_version.rb
|
data/README.md
CHANGED
@@ -1,286 +1,302 @@
|
|
1
|
-
# csvutils - tools 'n' scripts for working with comma-separated values (csv) datafiles - the world's most popular tabular
|
2
|
-
|
3
|
-
|
4
|
-
* home :: [github.com/
|
5
|
-
* bugs :: [github.com/
|
6
|
-
* gem :: [rubygems.org/gems/csvutils](https://rubygems.org/gems/csvutils)
|
7
|
-
* rdoc :: [rubydoc.info/gems/csvutils](http://rubydoc.info/gems/csvutils)
|
8
|
-
* forum :: [wwwmake](http://groups.google.com/group/wwwmake)
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
## Usage
|
13
|
-
|
14
|
-
### Command Line Tools
|
15
|
-
|
16
|
-
`csvhead` • `csvheader` • `csvstat` • `csvsplit` • `csvcut`
|
17
|
-
|
18
|
-
|
19
|
-
Try:
|
20
|
-
|
21
|
-
```
|
22
|
-
$ csvhead -h # or
|
23
|
-
$ csvhead --help
|
24
|
-
```
|
25
|
-
|
26
|
-
resulting in:
|
27
|
-
|
28
|
-
```
|
29
|
-
Usage: csvhead [OPTS] datafile ...
|
30
|
-
-n, --num=NUM Number of rows
|
31
|
-
-h, --help Prints this help
|
32
|
-
```
|
33
|
-
|
34
|
-
|
35
|
-
`csvcut -h` and so on.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
#### Working with Comma-Separated Values (CSV) Datafile Examples
|
40
|
-
|
41
|
-
Let's use a sample datafile e.g. `
|
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
|
-
|
202
|
-
```
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
```
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
|
229
|
-
|
230
|
-
| `
|
231
|
-
| `
|
232
|
-
| `
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
#
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
#
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
#
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
1
|
+
# csvutils - tools 'n' scripts for working with comma-separated values (csv) datafiles - the world's most popular tabular data interchange format in text
|
2
|
+
|
3
|
+
|
4
|
+
* home :: [github.com/csvreader/csvutils](https://github.com/csvreader/csvutils)
|
5
|
+
* bugs :: [github.com/csvreader/csvutils/issues](https://github.com/csvreader/csvutils/issues)
|
6
|
+
* gem :: [rubygems.org/gems/csvutils](https://rubygems.org/gems/csvutils)
|
7
|
+
* rdoc :: [rubydoc.info/gems/csvutils](http://rubydoc.info/gems/csvutils)
|
8
|
+
* forum :: [wwwmake](http://groups.google.com/group/wwwmake)
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
### Command Line Tools
|
15
|
+
|
16
|
+
`csvhead` • `csvheader` • `csvstat` • `csvsplit` • `csvcut`
|
17
|
+
|
18
|
+
|
19
|
+
Try the help option `-h/--help` with the command line tools. Example:
|
20
|
+
|
21
|
+
```
|
22
|
+
$ csvhead -h # or
|
23
|
+
$ csvhead --help
|
24
|
+
```
|
25
|
+
|
26
|
+
resulting in:
|
27
|
+
|
28
|
+
```
|
29
|
+
Usage: csvhead [OPTS] datafile ...
|
30
|
+
-n, --num=NUM Number of rows
|
31
|
+
-h, --help Prints this help
|
32
|
+
```
|
33
|
+
|
34
|
+
Now try it with `csvheader -h`, `csvstat -h`, `csvsplit -h`,
|
35
|
+
`csvcut -h` and so on.
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
#### Working with Comma-Separated Values (CSV) Datafile Examples
|
40
|
+
|
41
|
+
Let's use a sample datafile e.g. [`ENG.csv`](getting-started-samples/ENG.csv) from the
|
42
|
+
[football.csv project](https://github.com/footballcsv) with
|
43
|
+
matches from the English Premier League. Try
|
44
|
+
|
45
|
+
|
46
|
+
```
|
47
|
+
$ csvhead ENG.csv
|
48
|
+
```
|
49
|
+
to pretty print (pp) the first four rows (use the `-n/--num` option for more or less rows).
|
50
|
+
Resulting in:
|
51
|
+
|
52
|
+
```
|
53
|
+
== ENG.csv ==
|
54
|
+
|
55
|
+
<Date:11/08/17, Team1:Arsenal, Team2:Leicester, FT1:4, FT2:3>
|
56
|
+
<Date:12/08/17, Team1:Brighton, Team2:Man City, FT1:0, FT2:2>
|
57
|
+
<Date:12/08/17, Team1:Chelsea, Team2:Burnley, FT1:2, FT2:3>
|
58
|
+
<Date:12/08/17, Team1:Crystal Palace, Team2:Huddersfield, FT1:0, FT2:3>
|
59
|
+
4 rows
|
60
|
+
```
|
61
|
+
|
62
|
+
Next try
|
63
|
+
|
64
|
+
```
|
65
|
+
$ csvheader ENG.csv
|
66
|
+
```
|
67
|
+
|
68
|
+
to print all header columns (the first row). Resulting in:
|
69
|
+
|
70
|
+
```
|
71
|
+
== ENG.csv ==
|
72
|
+
|
73
|
+
5 columns:
|
74
|
+
1: Date
|
75
|
+
2: Team1
|
76
|
+
3: Team2
|
77
|
+
4: FT1
|
78
|
+
5: FT2
|
79
|
+
```
|
80
|
+
|
81
|
+
Next try:
|
82
|
+
|
83
|
+
```
|
84
|
+
$ csvstat -c Team1,Team2 ENG.csv
|
85
|
+
```
|
86
|
+
|
87
|
+
to show all unique values for the columns `Team1` and `Team2`.
|
88
|
+
Resulting in:
|
89
|
+
|
90
|
+
```
|
91
|
+
== ENG.csv ==
|
92
|
+
|
93
|
+
... 380 rows
|
94
|
+
|
95
|
+
5 columns:
|
96
|
+
1: Date
|
97
|
+
2: Team1
|
98
|
+
3: Team2
|
99
|
+
4: FT1
|
100
|
+
5: FT2
|
101
|
+
|
102
|
+
column "Team1" - 20 unique values:
|
103
|
+
19 x Arsenal
|
104
|
+
19 x Bournemouth
|
105
|
+
19 x Brighton
|
106
|
+
19 x Burnley
|
107
|
+
19 x Chelsea
|
108
|
+
19 x Crystal Palace
|
109
|
+
19 x Everton
|
110
|
+
19 x Huddersfield
|
111
|
+
19 x Leicester
|
112
|
+
19 x Liverpool
|
113
|
+
19 x Man City
|
114
|
+
19 x Man United
|
115
|
+
19 x Newcastle
|
116
|
+
19 x Southampton
|
117
|
+
19 x Stoke
|
118
|
+
19 x Swansea
|
119
|
+
19 x Tottenham
|
120
|
+
19 x Watford
|
121
|
+
19 x West Brom
|
122
|
+
19 x West Ham
|
123
|
+
column "Team2" - 20 unique values:
|
124
|
+
19 x Arsenal
|
125
|
+
19 x Bournemouth
|
126
|
+
19 x Brighton
|
127
|
+
19 x Burnley
|
128
|
+
19 x Chelsea
|
129
|
+
19 x Crystal Palace
|
130
|
+
19 x Everton
|
131
|
+
19 x Huddersfield
|
132
|
+
19 x Leicester
|
133
|
+
19 x Liverpool
|
134
|
+
19 x Man City
|
135
|
+
19 x Man United
|
136
|
+
19 x Newcastle
|
137
|
+
19 x Southampton
|
138
|
+
19 x Stoke
|
139
|
+
19 x Swansea
|
140
|
+
19 x Tottenham
|
141
|
+
19 x Watford
|
142
|
+
19 x West Brom
|
143
|
+
19 x West Ham
|
144
|
+
```
|
145
|
+
|
146
|
+
|
147
|
+
#### Split & Cut - Split One Datafile into Many or Cut / Reorder Columns
|
148
|
+
|
149
|
+
Let's use another sample datafile e.g. [`AUT.csv`](getting-started-samples/AUT.csv) that holds many seasons
|
150
|
+
from the Austrian (AUT) Bundesliga. First lets see how many seasons:
|
151
|
+
|
152
|
+
```
|
153
|
+
$ csvstat -c Season AUT.csv
|
154
|
+
```
|
155
|
+
|
156
|
+
Resulting in:
|
157
|
+
|
158
|
+
```
|
159
|
+
== AUT.csv ==
|
160
|
+
|
161
|
+
... 360 rows
|
162
|
+
|
163
|
+
6 columns:
|
164
|
+
1: Season
|
165
|
+
2: Date
|
166
|
+
3: Team1
|
167
|
+
4: Team2
|
168
|
+
5: FT1
|
169
|
+
6: FT2
|
170
|
+
|
171
|
+
column "Season" - 2 unique values:
|
172
|
+
180 x 2016/2017
|
173
|
+
180 x 2017/2018
|
174
|
+
```
|
175
|
+
|
176
|
+
Now let's split the `AUT.csv` datafile by the `Season` column
|
177
|
+
resulting in two new datafiles named `AUT_2016-2017.csv`
|
178
|
+
and `ÀUT_2017-2018.csv`. Try:
|
179
|
+
|
180
|
+
```
|
181
|
+
$ csvsplit -c Season AUT.csv
|
182
|
+
```
|
183
|
+
|
184
|
+
Resulting in:
|
185
|
+
|
186
|
+
```
|
187
|
+
new chunk: ["2016/2017"] - saving "AUT_2016-2017.csv"...
|
188
|
+
new chunk: ["2017/2018"] - saving "AUT_2017-2018.csv"...
|
189
|
+
```
|
190
|
+
|
191
|
+
Let's cut out (remove) the `Season` column from the new `AUT_2016-2017.csv`
|
192
|
+
datafile. Try:
|
193
|
+
|
194
|
+
```
|
195
|
+
$ csvcut -c Date,Team1,Team2,FT1,FT2 AUT_2016-2017.csv
|
196
|
+
```
|
197
|
+
|
198
|
+
Double check the overwritten in-place cleaned-up datafile:
|
199
|
+
|
200
|
+
```
|
201
|
+
$ csvhead AUT_2016-2017.csv
|
202
|
+
```
|
203
|
+
|
204
|
+
resulting in:
|
205
|
+
|
206
|
+
```
|
207
|
+
== AUT_2016-2017.csv ==
|
208
|
+
|
209
|
+
<Date:23/07/16, Team1:Rapid Vienna, Team2:Ried, FT1:5, FT2:0>
|
210
|
+
<Date:23/07/16, Team1:Altach, Team2:AC Wolfsberger, FT1:1, FT2:0>
|
211
|
+
<Date:23/07/16, Team1:Sturm Graz, Team2:Salzburg, FT1:3, FT2:1>
|
212
|
+
<Date:24/07/16, Team1:St. Pölten, Team2:Austria Vienna, FT1:1, FT2:2>
|
213
|
+
4 rows
|
214
|
+
```
|
215
|
+
|
216
|
+
And so on and so forth.
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
### Code, Code, Code - Script Your Data Work Flow with Ruby
|
222
|
+
|
223
|
+
You can use all tools in your script using the `CsvUtils`
|
224
|
+
class methods:
|
225
|
+
|
226
|
+
| Shell | Ruby |
|
227
|
+
|-------------|-----------------------------------|
|
228
|
+
| `csvhead` | [`CsvUtils.head( path, n: 4 )`](lib/csvutils/head.rb) |
|
229
|
+
| `csvheader` | [`CsvUtils.header( path )`](lib/csvutils/header.rb) |
|
230
|
+
| `csvstat` | [`CsvUtils.stat( path, *columns )`](lib/csvutils/stat.rb) |
|
231
|
+
| `csvsplit` | [`CsvUtils.split( path, *columns )`](lib/csvutils/split.rb) |
|
232
|
+
| `csvcut` | [`CsvUtils.cut( path, *columns, output: path)`](lib/csvutils/cut.rb) |
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
Let's retry the sample above in a script:
|
237
|
+
|
238
|
+
|
239
|
+
``` ruby
|
240
|
+
require 'csvutils'
|
241
|
+
|
242
|
+
|
243
|
+
CsvUtils.head( 'ENG.csv' )
|
244
|
+
# same as:
|
245
|
+
# $ csvhead ENG.csv
|
246
|
+
|
247
|
+
CsvUtils.header( 'ENG.csv' )
|
248
|
+
# same as:
|
249
|
+
# $ csvheader ENG.csv
|
250
|
+
|
251
|
+
CsvUtils.stat( 'ENG.csv', 'Team1', 'Team2' )
|
252
|
+
# same as:
|
253
|
+
# $ csvstat -c Team1,Team2 ENG.csv
|
254
|
+
|
255
|
+
|
256
|
+
CsvUtils.stat( 'AUT.csv', 'Season' )
|
257
|
+
# same as:
|
258
|
+
# $ csvstat -c Season AUT.csv
|
259
|
+
|
260
|
+
CsvUtils.split( 'AUT.csv', 'Season' )
|
261
|
+
# same as:
|
262
|
+
# $ csvsplit -c Season AUT.csv
|
263
|
+
|
264
|
+
CsvUtils.cut( 'AUT_2016-2017.csv', 'Date', 'Team1', 'Team2', 'FT1', 'FT2' )
|
265
|
+
# same as:
|
266
|
+
# $ csvcut -c Date,Team1,Team2,FT1,FT2 AUT_2016-2017.csv
|
267
|
+
```
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
That's it. See the [`/getting-started-samples`](/getting-started-samples)
|
272
|
+
folder to
|
273
|
+
run the samples on your own computer.
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
## Install
|
278
|
+
|
279
|
+
Just install the gem:
|
280
|
+
|
281
|
+
```
|
282
|
+
$ gem install csvutils
|
283
|
+
```
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
## Alternatives
|
288
|
+
|
289
|
+
See the Libraries & Tools section in the [Awesome CSV](https://github.com/csvspecs/awesome-csv#libraries--tools) page.
|
290
|
+
|
291
|
+
|
292
|
+
## License
|
293
|
+
|
294
|
+
![](https://publicdomainworks.github.io/buttons/zero88x31.png)
|
295
|
+
|
296
|
+
The `csvutils` scripts are dedicated to the public domain.
|
297
|
+
Use it as you please with no restrictions whatsoever.
|
298
|
+
|
299
|
+
## Questions? Comments?
|
300
|
+
|
301
|
+
Send them along to the [wwwmake forum](http://groups.google.com/group/wwwmake).
|
302
|
+
Thanks!
|