csv-utils 0.3.19 → 0.3.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/csv-grep +91 -0
- data/csv-utils.gemspec +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bea28dcd86e140064eba5a1134d5aa6193963ddcb7632c63c2e1bfa85c4889ed
|
4
|
+
data.tar.gz: 1547e2cb2b937c580031205baa64c79c3c0ca062ee2aeffd578b897d10fae092
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe635fdc22220a0377ca386132109bebce61ba5fd41ec71625b8940b4dd7e2e5c72efb8cf163fc44f990633e18b8024b14878cbe0e852b49d4ba31fb076e44b3
|
7
|
+
data.tar.gz: 569285d118ead894808ca96b5e4d1c0c1dd5f4bd130452a79c1ffb4f987ede9a0cf324609f4d11a15bddf61169948799026dd2bd4836674a1aae2f2caa137063
|
data/bin/csv-grep
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
options = {
|
6
|
+
search: nil,
|
7
|
+
exact_match: false,
|
8
|
+
headers: :first,
|
9
|
+
limit: nil,
|
10
|
+
search_regex_options: nil
|
11
|
+
}
|
12
|
+
OptionParser.new do |opts|
|
13
|
+
opts.banner = 'Usage: ' + File.basename(__FILE__) + ' [options] <csv file>'
|
14
|
+
|
15
|
+
opts.on('-h', '--help', 'Prints this help') do
|
16
|
+
puts opts
|
17
|
+
exit
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on('-s', '--search SEARCH', 'Search expression') do |v|
|
21
|
+
options[:search] = v.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
opts.on('-e', '--exact-match', 'Exact match') do
|
25
|
+
options[:exact_match] = true
|
26
|
+
end
|
27
|
+
|
28
|
+
# -c stands for column, since -h is used for help
|
29
|
+
opts.on('-c', '--headers HEADERS', 'Comma separated list of headers to search (default first column)') do |v|
|
30
|
+
options[:headers] = v == 'all' ? :all : v.split(',')
|
31
|
+
end
|
32
|
+
|
33
|
+
opts.on('-l', '--limit LIMIT', Integer, 'Limit the number of matches') do |v|
|
34
|
+
options[:limit] = v
|
35
|
+
end
|
36
|
+
|
37
|
+
opts.on('-i', '--ignore-case', 'Ignore case') do |v|
|
38
|
+
options[:search_regex_options] = Regexp::IGNORECASE
|
39
|
+
end
|
40
|
+
end.parse!
|
41
|
+
|
42
|
+
raise('no search specified') unless options[:search]
|
43
|
+
|
44
|
+
require 'csv-utils'
|
45
|
+
|
46
|
+
csv = CSVUtils::CSVIterator.new(ARGV[0])
|
47
|
+
|
48
|
+
search_regex =
|
49
|
+
if options[:exact_match]
|
50
|
+
Regexp.new('\A' + Regexp.escape(options[:search]) + '\z', options[:search_regex_options])
|
51
|
+
else
|
52
|
+
Regexp.new(options[:search], options[:search_regex_options])
|
53
|
+
end
|
54
|
+
|
55
|
+
headers =
|
56
|
+
case options[:headers]
|
57
|
+
when :first
|
58
|
+
[csv .headers.first]
|
59
|
+
when :all
|
60
|
+
csv.headers
|
61
|
+
else
|
62
|
+
options[:headers]
|
63
|
+
end
|
64
|
+
|
65
|
+
missing_headers = headers - csv.headers
|
66
|
+
raise("unknown headers #{headers.join(', ')}") unless missing_headers.empty?
|
67
|
+
|
68
|
+
matching_row_proc = proc do |row|
|
69
|
+
result = false
|
70
|
+
|
71
|
+
headers.each do |header|
|
72
|
+
next unless (val = row[header])
|
73
|
+
|
74
|
+
if search_regex.match?(val)
|
75
|
+
result = true
|
76
|
+
break
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
result
|
81
|
+
end
|
82
|
+
|
83
|
+
matches = 0
|
84
|
+
csv.each do |row|
|
85
|
+
next unless matching_row_proc.call(row)
|
86
|
+
|
87
|
+
matches += 1
|
88
|
+
print row.to_pretty_s + "\n"
|
89
|
+
|
90
|
+
break if options[:limit] && matches >= options[:limit]
|
91
|
+
end
|
data/csv-utils.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.21
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Doug Youch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: inheritance-helper
|
@@ -32,6 +32,7 @@ executables:
|
|
32
32
|
- csv-duplicate-finder
|
33
33
|
- csv-explorer
|
34
34
|
- csv-find-error
|
35
|
+
- csv-grep
|
35
36
|
- csv-readline
|
36
37
|
- csv-validator
|
37
38
|
extensions: []
|
@@ -49,6 +50,7 @@ files:
|
|
49
50
|
- bin/csv-duplicate-finder
|
50
51
|
- bin/csv-explorer
|
51
52
|
- bin/csv-find-error
|
53
|
+
- bin/csv-grep
|
52
54
|
- bin/csv-readline
|
53
55
|
- bin/csv-validator
|
54
56
|
- csv-utils.gemspec
|