Skip to content

Commit ef92b14

Browse files
committed
update doc.
1 parent f50cb01 commit ef92b14

File tree

3 files changed

+182
-76
lines changed

3 files changed

+182
-76
lines changed

README

+135-76
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,142 @@ Unix filter like operations (grep, sort, cat, etc.),
55
SQL like operations (join, group, etc.),
66
and more.
77

8-
tb supports CSV and TSV for inputs.
9-
tb basically generates CSV but it has conversion tool to TSV, JSON, YAML.
8+
== Example
9+
10+
There is a CSV file for programming languages and their birth year in
11+
sample/ directory in tb package.
12+
13+
% head sample/langs.csv
14+
language,year
15+
FORTRAN,1955
16+
LISP,1958
17+
COBOL,1959
18+
ALGOL 58,1958
19+
APL,1962
20+
Simula,1962
21+
SNOBOL,1962
22+
BASIC,1964
23+
PL/I,1964
24+
25+
You can sort a CSV file using "tb" command with "sort" subcommand.
26+
You don't need to care header: header is retained as is.
27+
28+
% tb sort sample/langs.csv|head
29+
language,year
30+
ALGOL 58,1958
31+
APL,1962
32+
Ada,1983
33+
B,1969
34+
BASIC,1964
35+
BCPL,1967
36+
C,1972
37+
C#,2001
38+
C++,1980
39+
40+
You can specify a field to sort.
41+
You don't need to count the position of the field.
42+
43+
% tb sort -f year sample/langs.csv|head
44+
language,year
45+
FORTRAN,1955
46+
LISP,1958
47+
ALGOL 58,1958
48+
COBOL,1959
49+
APL,1962
50+
SNOBOL,1962
51+
Simula,1962
52+
BASIC,1964
53+
PL/I,1964
54+
55+
You can search CSV file using "grep" subcommand.
56+
57+
% tb grep 'R' sample/langs.csv
58+
language,year
59+
FORTRAN,1955
60+
Ruby,1993
61+
62+
You can search languages which name contains characters other than alphabet.
63+
You don't need to care field separators (comma).
64+
65+
% tb grep -vf language '\A[A-Za-z]*\z' sample/langs.csv |cat
66+
language,year
67+
ALGOL 58,1958
68+
PL/I,1964
69+
C++,1980
70+
Objective-C,1983
71+
Common Lisp,1984
72+
Visual Basic,1991
73+
C#,2001
74+
F#,2002
75+
76+
You can search languages using Ruby expression, instead of a regexp.
77+
78+
% tb grep --ruby '(1990..1999).include?(_["year"].to_i)' sample/langs.csv
79+
language,year
80+
Haskell,1990
81+
Python,1991
82+
Visual Basic,1991
83+
Ruby,1993
84+
Lua,1993
85+
CLOS,1994
86+
Java,1995
87+
Delphi,1995
88+
JavaScript,1995
89+
PHP,1995
90+
D,1999
91+
92+
You can extract language names using "select" subcommand.
93+
This is similar to "cut" command of Unix and projection of relational algebra.
94+
95+
% tb select language sample/langs.csv |head
96+
language
97+
FORTRAN
98+
LISP
99+
COBOL
100+
ALGOL 58
101+
APL
102+
Simula
103+
SNOBOL
104+
BASIC
105+
PL/I
106+
107+
You can group languages for each year using "group" subcommand.
108+
109+
% tb group year -a count -a 'values(language)' sample/langs.csv |head
110+
year,count,values(language)
111+
1955,1,FORTRAN
112+
1958,2,"LISP,ALGOL 58"
113+
1959,1,COBOL
114+
1962,3,"APL,Simula,SNOBOL"
115+
1964,2,"BASIC,PL/I"
116+
1967,1,BCPL
117+
1968,1,Logo
118+
1969,1,B
119+
1970,2,"Pascal,Forth"
120+
121+
You can see list of subcommand using "help" subcommand.
10122

11-
tb interprets a table similar to RDB table:
12-
list of rows which contains fields identified by field names.
13-
The field names are taken from the first (non-empty) row of the table.
14-
There is a way to choose another manner:
15-
field names are defined by numerical column index begins with 1.
123+
% tb help
124+
Usage:
125+
tb help [OPTS] [SUBCOMMAND]
126+
tb csv [OPTS] [TABLE]
127+
tb tsv [OPTS] [TABLE]
128+
tb json [OPTS] [TABLE]
129+
tb yaml [OPTS] [TABLE]
130+
tb pp [OPTS] [TABLE]
131+
tb grep [OPTS] REGEXP [TABLE]
132+
tb gsub [OPTS] REGEXP STRING [TABLE]
133+
tb sort [OPTS] [TABLE]
134+
tb select [OPTS] FIELD,... [TABLE]
135+
tb rename [OPTS] SRC,DST,... [TABLE]
136+
tb newfield [OPTS] FIELD RUBY-EXP [TABLE]
137+
tb cat [OPTS] [TABLE ...]
138+
tb join [OPTS] [TABLE ...]
139+
tb group [OPTS] KEY-FIELD1,... [TABLE]
140+
tb cross [OPTS] HKEY-FIELD1,... VKEY-FIELD1,... [TABLE]
141+
tb shape [OPTS] [TABLE ...]
142+
tb mheader [OPTS] [TABLE]
143+
tb crop [OPTS] [TABLE]
16144

17145
== Command Line Tool
18146

@@ -38,75 +166,6 @@ tb command has many subcommands.
38166
mheader : collapse multi rows header.
39167
crop : extract rectangle in a table.
40168

41-
== Format Conversion
42-
43-
For example, a JSON can be generated from a CSV as follows.
44-
45-
% cat tst.csv
46-
a,b,c
47-
0,1,2
48-
4,5,6
49-
% tb json tst.csv
50-
[{
51-
"a": "0",
52-
"b": "1",
53-
"c": "2"
54-
},
55-
{
56-
"a": "4",
57-
"b": "5",
58-
"c": "6"
59-
}]
60-
61-
pp subcommand provides a compact format.
62-
63-
% tb pp tst.csv
64-
{"a"=>"0", "b"=>"1", "c"=>"2"}
65-
{"a"=>"4", "b"=>"5", "c"=>"6"}
66-
67-
tsv subcommand generates a TSV.
68-
69-
% tb tsv tst.csv > tst.tsv
70-
% cat -t tst.tsv
71-
a^Ib^Ic
72-
0^I1^I2
73-
4^I5^I6
74-
75-
tb accepts TSV as inputs as well.
76-
77-
% tb yaml tst.tsv
78-
---
79-
- a: '0'
80-
b: '1'
81-
c: '2'
82-
- a: '4'
83-
b: '5'
84-
c: '6'
85-
86-
== Usage
87-
88-
% tb help
89-
Usage:
90-
tb help [OPTS] [SUBCOMMAND]
91-
tb csv [OPTS] [TABLE]
92-
tb tsv [OPTS] [TABLE]
93-
tb json [OPTS] [TABLE]
94-
tb yaml [OPTS] [TABLE]
95-
tb pp [OPTS] [TABLE]
96-
tb grep [OPTS] REGEXP [TABLE]
97-
tb gsub [OPTS] REGEXP STRING [TABLE]
98-
tb sort [OPTS] [TABLE]
99-
tb select [OPTS] FIELD,... [TABLE]
100-
tb rename [OPTS] SRC,DST,... [TABLE]
101-
tb newfield [OPTS] FIELD RUBY-EXP [TABLE]
102-
tb cat [OPTS] [TABLE ...]
103-
tb join [OPTS] [TABLE ...]
104-
tb group [OPTS] KEY-FIELD1,... [TABLE]
105-
tb cross [OPTS] HKEY-FIELD1,... VKEY-FIELD1,... [TABLE]
106-
tb shape [OPTS] [TABLE ...]
107-
tb mheader [OPTS] [TABLE]
108-
tb crop [OPTS] [TABLE]
109-
110169
== Install
111170

112171
gem install tb

sample/langs.csv

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
language,year
2+
FORTRAN,1955
3+
LISP,1958
4+
COBOL,1959
5+
ALGOL 58,1958
6+
APL,1962
7+
Simula,1962
8+
SNOBOL,1962
9+
BASIC,1964
10+
PL/I,1964
11+
BCPL,1967
12+
Logo,1968
13+
B,1969
14+
Pascal,1970
15+
Forth,1970
16+
C,1972
17+
Smalltalk,1972
18+
Prolog,1972
19+
ML,1973
20+
Scheme,1975
21+
SQL,1978
22+
C++,1980
23+
Objective-C,1983
24+
Ada,1983
25+
Common Lisp,1984
26+
Eiffel,1985
27+
Erlang,1986
28+
Perl,1987
29+
Tcl,1988
30+
Haskell,1990
31+
Python,1991
32+
Visual Basic,1991
33+
Ruby,1993
34+
Lua,1993
35+
CLOS,1994
36+
Java,1995
37+
Delphi,1995
38+
JavaScript,1995
39+
PHP,1995
40+
D,1999
41+
C#,2001
42+
F#,2002
43+
Groovy,2003
44+
Scala,2003
45+
Clojure,2007
46+
Go,2009

tb.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
2020
sample/excel2csv
2121
sample/poi-xls2csv.rb
2222
sample/poi-xls2csv.sh
23+
sample/langs.csv
2324
test-all.rb
2425
test-all-cov.rb
2526
]

0 commit comments

Comments
 (0)