csv_filter 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +18 -0
- data/VERSION +1 -1
- data/csv_filter.gemspec +2 -2
- data/lib/csv_filter.rb +17 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -2,6 +2,24 @@
|
|
2
2
|
|
3
3
|
Filters CSV or TSV files (default) by column names.
|
4
4
|
|
5
|
+
== CLI Usage
|
6
|
+
|
7
|
+
csv_filter comes with a command line utility conveniently called csv_filter. To use it:
|
8
|
+
`csv_filter path_to_file`
|
9
|
+
|
10
|
+
When prompted, enter the names of the columns (fields) you'd like to limit your results to.
|
11
|
+
|
12
|
+
Example:
|
13
|
+
|
14
|
+
<pre>
|
15
|
+
csv_filter spec/sample.tsv
|
16
|
+
available fields are: ["value", "type", "url", "format"]
|
17
|
+
enter field names to filter
|
18
|
+
type format
|
19
|
+
0. URL RAW
|
20
|
+
1. URL RAW
|
21
|
+
</pre>
|
22
|
+
|
5
23
|
== Contributing to csv_filter
|
6
24
|
|
7
25
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/csv_filter.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "csv_filter"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kris Luminar"]
|
12
|
-
s.date = "2012-12-
|
12
|
+
s.date = "2012-12-12"
|
13
13
|
s.description = "Filters CSV or TSV files (default) by column names"
|
14
14
|
s.email = "kris.luminar@gmail.com"
|
15
15
|
s.executables = ["csv_filter"]
|
data/lib/csv_filter.rb
CHANGED
@@ -10,6 +10,8 @@ class CsvFilter
|
|
10
10
|
grab_header
|
11
11
|
end
|
12
12
|
|
13
|
+
# grab the first row of the inputed csv file
|
14
|
+
#
|
13
15
|
def grab_header
|
14
16
|
return @header if (@header and !@header.empty?)
|
15
17
|
@file.rewind
|
@@ -19,15 +21,22 @@ class CsvFilter
|
|
19
21
|
@header
|
20
22
|
end
|
21
23
|
|
24
|
+
# the positions of all the columns that the user wants filtered on
|
25
|
+
#
|
22
26
|
def filtered_column_positions columns
|
23
27
|
columns = columns.flatten
|
24
28
|
@filtered_column_positions ||= register.select {|field, pos| columns.include? field }.values
|
25
29
|
end
|
26
30
|
|
31
|
+
# map column names to their relative positions (numbers)
|
32
|
+
#
|
27
33
|
def register
|
28
34
|
grab_header.invert
|
29
35
|
end
|
30
36
|
|
37
|
+
# selects just the desired columns from a CSV file
|
38
|
+
#
|
39
|
+
# @param columns list of columns to filter on
|
31
40
|
def filter(*columns)
|
32
41
|
# columns = [*columns].flatten #columns should accept either an array of strings or a variable number of strings
|
33
42
|
raise ArgumentError unless (columns.respond_to?(:size) and columns.size < @num_columns)
|
@@ -46,6 +55,9 @@ class CsvFilter
|
|
46
55
|
output
|
47
56
|
end
|
48
57
|
|
58
|
+
# CLI interface for filter
|
59
|
+
#
|
60
|
+
# (see #filter)
|
49
61
|
def print_filter(columns)
|
50
62
|
lines = filter(columns)
|
51
63
|
output = []
|
@@ -59,10 +71,15 @@ class CsvFilter
|
|
59
71
|
puts output.join("\n")
|
60
72
|
end
|
61
73
|
|
74
|
+
# count num of columns
|
75
|
+
# already deprecated, always a bad sign
|
76
|
+
#
|
62
77
|
def count_columns
|
63
78
|
fields.size
|
64
79
|
end
|
65
80
|
|
81
|
+
# list the fields from the header rown in the input file
|
82
|
+
#
|
66
83
|
def fields
|
67
84
|
return @fields if @fields
|
68
85
|
@file.rewind
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -132,7 +132,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
132
|
version: '0'
|
133
133
|
segments:
|
134
134
|
- 0
|
135
|
-
hash:
|
135
|
+
hash: 384472246441890925
|
136
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
137
|
none: false
|
138
138
|
requirements:
|