dgrep 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4b5108c5653d5773abfdff18f359a46937bdecb
4
+ data.tar.gz: a23bb4217d3cac50030035dc3ecf00017385fb5c
5
+ SHA512:
6
+ metadata.gz: df427ec0b65008863ad4990a85c3a1afff464023d1185589846e1f6900123fbe72075948c10efc2f643480db793e9f0e93bda840631a24b320e7866f46336e04
7
+ data.tar.gz: 4973e023b8e0ab91cecf989e7120c9c22def5a780c4f18ca706302f971acecc0a94726bf44443bf226b4f0a0e31f0d3cdf67f2dcf579b023d5ef7dfc6b7d8380
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dgrep.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nishimoto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Dgrep
2
+
3
+ dgrep is grep program for tsv or csv file.
4
+
5
+ ## Installation
6
+
7
+ install it yourself as:
8
+
9
+ $ gem install dgrep
10
+
11
+ ## Usage
12
+
13
+ If there is this file
14
+
15
+ ```:sample.txt
16
+ a b c
17
+ d 1 2 3
18
+ e 4 5 6
19
+ f 7 8 9
20
+ ```
21
+
22
+ and you want to get this output,
23
+
24
+
25
+ ```
26
+ a b c
27
+ d 1 2 3
28
+ ```
29
+
30
+ you only do
31
+
32
+ ```
33
+ $ dgrep "d" sample.txt
34
+ ```
35
+
36
+ if you wan to get this outpus,
37
+
38
+ ```
39
+ a
40
+ d 1
41
+ e 4
42
+ f 7
43
+ ```
44
+
45
+ you only do
46
+
47
+ ```
48
+ dgrep -c "a" sample.txt
49
+ ```
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nishimoto/dgrep.
54
+
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
59
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dgrep"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/dgrep ADDED
@@ -0,0 +1,224 @@
1
+ #!/usr/bin/env ruby
2
+ # require library
3
+ require "dgrep"
4
+ require "optparse"
5
+ require "csv"
6
+ require "pry"
7
+ require "json"
8
+
9
+ ###################################################
10
+ # over ride
11
+ class Fixnum
12
+ def must(str, num)
13
+ if str == "be greater than"
14
+ if self <= num
15
+ raise "dgrep: You need #{num+1} or more argv\n"
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class Array
22
+
23
+ def sum
24
+ self.inject{|sum, n| sum + n }
25
+ end
26
+
27
+ def mean
28
+ self.inject(0.0){|r,i| r+=i }/self.size.to_f
29
+ end
30
+
31
+ def median
32
+ self.size % 2 == 0 ? self[self.size/2 - 1, 2].inject(:+) / 2.0 : self[self.size/2]
33
+ end
34
+
35
+ def to_f
36
+ self.map { |e| e.to_i }
37
+ end
38
+
39
+ def to_f
40
+ self.map { |e| e.to_f }
41
+ end
42
+ end
43
+
44
+
45
+ ###################################################
46
+ # definitions.
47
+ def optparse
48
+ hash = Hash.new()
49
+ hash2 = Hash.new() # 使用されているかどうか
50
+ opts = OptionParser.new
51
+
52
+ opts.on("-c") do |opt|
53
+ hash[:c] = true
54
+ hash2[:c] = true
55
+ end
56
+
57
+ opts.on("-d [VAL]") do |opt|
58
+ hash[:sep] = opt
59
+ hash2[:sep] = true
60
+ end
61
+
62
+ opts.on("-e [VAL]" ) do |opt|
63
+ hash[:e] = opt
64
+ hash2[:e] = true
65
+ end
66
+
67
+ opts.on("-h [VAL]") do |opt|
68
+ if opt == "false" || opt == "F"
69
+ hash[:h] = false
70
+ elsif opt == "true" || opt == "T"
71
+ hash[:h] = true
72
+ end
73
+ hash2[:h] = true
74
+ end
75
+
76
+ # quote
77
+ opts.on("-q [VAL]") do |opt|
78
+ hash[:q] = opt
79
+ hash2[:q] = true
80
+ end
81
+
82
+ opts.on("-i [VAL]") do |opt|
83
+ hash[:i] = opt
84
+ hash2[:i] = true
85
+ end
86
+
87
+ opts.on("--config") do |opt|
88
+ hash[:set_conf_mode] = true
89
+ hash2[:set_conf_mode] = true
90
+ end
91
+
92
+ opts.on("--get_config") do |opt|
93
+ hash2[:get_conf_mode] = true
94
+ end
95
+
96
+ opts.parse!
97
+
98
+ hash[:sep] ||= "\t"
99
+ # hash[:h] ||= true
100
+ hash[:q] ||= "\""
101
+ hash[:i] ||= 0
102
+ return [hash, hash2]
103
+ end
104
+
105
+ def print_2d_array(arr_2d, conf)
106
+ arr_2d.each do |arr|
107
+ print CSV.generate_line(arr, col_sep: conf[:sep], quote_char: conf[:q])
108
+ end
109
+ end
110
+
111
+ def opfile_name(filename)
112
+ "#{filename.split(".")[0..-2].join(".")}.t.#{filename.split(".")[-1]}"
113
+ end
114
+
115
+ def tranpose_tsv(filename, sep)
116
+ CSV.open(opfile_name(filename), "w", col_sep: sep) do |csv|
117
+ CSV.read(filename, col_sep: sep).transpose.each do |arr|
118
+ csv << arr
119
+ end
120
+ end
121
+ end
122
+
123
+ def set_conf_to_config_file(conf_filename, conf)
124
+ File.open(conf_filename, "w") do |file|
125
+ str = JSON.dump(conf, file)
126
+ end
127
+ end
128
+
129
+ def get_conf_from_config_file(conf_filename, conf, conf_used)
130
+ hash = JSON.load(File.open(conf_filename, "r"))
131
+ hash.each do |k,v|
132
+ if !conf_used[k.to_sym]
133
+ conf[k.to_sym] = hash[k]
134
+ # as in Json.dump convert false to "false" in set_conf_to_config_file function.
135
+ conf[k.to_sym] = false if hash[k] == "false"
136
+ conf[k.to_sym] = true if hash[k] == "true"
137
+ end
138
+ end
139
+ return conf
140
+ end
141
+
142
+ ###################################################
143
+ # opt parse
144
+ conf, conf_used = optparse
145
+ conf_filename = File.expand_path("../../lib/conf.json", __FILE__)
146
+ if conf[:set_conf_mode]
147
+ set_conf_to_config_file(conf_filename, conf)
148
+ exit
149
+ else
150
+ conf = get_conf_from_config_file(conf_filename, conf, conf_used)
151
+ end
152
+
153
+ if conf_used[:get_conf_mode]
154
+ p conf
155
+ exit
156
+ end
157
+
158
+ # This program need greped_str, grep_str
159
+ greped_str_filename_array = []
160
+ if conf[:e]
161
+ ARGV.length.must("be greater than", 0)
162
+ greped_str_filename_array = ARGV[0..-1]
163
+ else
164
+ ARGV.length.must("be greater than", 1)
165
+ greped_str_filename_array = ARGV[1..-1]
166
+ end
167
+
168
+ grep_str = ARGV[0]
169
+
170
+ ###################################################
171
+ # CSV.parse
172
+ greped_str_filename_array.each do |filename|
173
+ # transpose tsv if -c option.
174
+ if conf[:c]
175
+ tranpose_tsv(filename, conf[:sep])
176
+ filename = opfile_name(filename)
177
+ end
178
+
179
+ # grep main function.
180
+ file = File.open(filename,"r")
181
+
182
+ # main array for output.
183
+ output_array = []
184
+
185
+ # process header
186
+ if conf[:h]
187
+ # p "hogehoge"
188
+ header_array = CSV.parse(file.gets, col_sep: conf[:sep], quote_char: conf[:q])
189
+ output_array.push header_array[0]
190
+ end
191
+
192
+ if conf[:i]
193
+ index = conf[:i].to_i
194
+ there_is_index = true
195
+ else
196
+ there_is_index = false
197
+ end
198
+
199
+ CSV.parse(file, col_sep: conf[:sep], quote_char: conf[:q]).each do |arr|
200
+ line = CSV.generate_line(arr, col_sep: conf[:sep], quote_char: conf[:q])
201
+
202
+ if there_is_index
203
+ rowname = arr[index]
204
+ colname = rowname
205
+ arr.delete_at(index)
206
+ end
207
+
208
+ if conf_used[:e]
209
+ if eval(conf[:e])
210
+ arr.insert(index, rowname) if there_is_index
211
+ output_array.push arr
212
+ end
213
+ else
214
+ if line.index(grep_str)
215
+ arr.insert(index, rowname) if there_is_index
216
+ output_array.push arr
217
+ end
218
+ end
219
+ end
220
+ if conf[:c]
221
+ output_array = output_array.transpose
222
+ end
223
+ print_2d_array(output_array, conf)
224
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/dgrep.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dgrep/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dgrep"
8
+ spec.version = Dgrep::VERSION
9
+ spec.authors = ["Nishimoto"]
10
+ spec.email = ["thinkn1108@gmail.com"]
11
+
12
+ spec.summary = %q{grep tsv,csv file}
13
+ spec.description = %q{grep tsv,csv file}
14
+ spec.homepage = "https://github.com/nishimoto/dgrep"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
data/exe/dgrep ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "dgrep"
4
+ require "thor"
5
+
6
+ module Dgrep
7
+ class CLI < Thor
8
+ default_task :hello
9
+ def hello
10
+ p Dir.pwd
11
+ end
12
+
13
+ end
14
+ end
data/lib/conf.json ADDED
@@ -0,0 +1 @@
1
+ {"set_conf_mode":true,"h":"true","sep":"\t","q":"\"","i":0}
data/lib/dgrep.rb ADDED
@@ -0,0 +1 @@
1
+ require "dgrep/version"
@@ -0,0 +1,3 @@
1
+ module Dgrep
2
+ VERSION = "0.1.0"
3
+ end
data/temp.txt ADDED
@@ -0,0 +1,100 @@
1
+ A-1 58 73 84 94 85 27 394 O
2
+ A-2 30 53 56 68 55 34 262 O
3
+ A-3 49 77 75 84 63 23 348 O
4
+ A-4 57 76 82 95 78 22 388 O
5
+ A-5 65 90 77 88 79 25 399 O
6
+ A-6 42 60 65 78 62 42 307 O
7
+ A-7 44 61 72 83 61 28 321 O
8
+ A-8 34 59 57 68 56 34 274 O
9
+ A-9 52 67 83 92 74 11 368 O
10
+ A-10 45 66 69 85 59 23 324 O
11
+ A-11 59 72 73 86 74 23 364 O
12
+ A-12 57 81 100 100 76 414 O
13
+ A-13 52 67 81 94 72 28 366 O
14
+ A-14 35 53 74 83 65 28 310 O
15
+ A-15 46 74 66 78 56 22 320 O
16
+ A-16 56 68 80 92 68 27 364 O
17
+ A-17 66 81 70 80 77 29 374 O
18
+ A-18 31 67 56 67 48 33 269 O
19
+ A-19 61 77 76 89 71 27 374 O
20
+ A-20 50 53 71 83 71 35 328 O
21
+ A-21 53 71 81 97 67 25 369 O
22
+ A-22 61 77 84 90 76 388 O
23
+ A-23 46 72 67 84 60 29 329 O
24
+ A-24 52 72 56 79 65 51 324 X
25
+ A-25 62 86 74 94 69 39 385 O
26
+ A-26 45 64 74 80 69 24 332 X
27
+ A-27 43 69 73 80 56 12 321 O
28
+ A-28 44 67 66 77 59 20 313 O
29
+ A-29 45 62 49 69 52 44 277 O
30
+ A-30 57 76 72 86 80 29 371 O
31
+ A-31 32 59 68 76 59 18 294 O
32
+ A-32 46 74 80 91 70 31 361 O
33
+ A-33 55 73 80 88 76 12 372 O
34
+ A-34 31 60 62 75 56 25 284 O
35
+ A-35 36 58 68 70 59 15 291 O
36
+ A-36 36 68 68 78 58 26 308 O
37
+ A-37 37 49 72 73 58 13 289 O
38
+ A-38 61 71 94 103 81 17 410 O
39
+ A-39 28 60 58 68 48 43 262 O
40
+ A-40 53 71 73 81 70 6 348 O
41
+ A-41 41 64 62 76 54 29 297 O
42
+ A-42 52 66 70 72 72 11 332 X
43
+ A-43 36 58 63 69 60 12 286 O
44
+ A-44 67 80 82 95 91 35 415 O
45
+ A-45 56 68 69 86 67 39 346 O
46
+ A-46 47 71 74 90 63 37 345 O
47
+ A-47 45 71 83 87 58 5 344 X
48
+ A-48 59 67 72 83 79 33 360 O
49
+ A-49 55 75 94 96 81 3 401 O
50
+ A-50 34 63 56 56 57 6 266 O
51
+ B-1 51 82 65 78 64 15 340 O
52
+ B-2 48 60 77 95 65 37 345 O
53
+ B-3 56 78 82 95 73 16 384 O
54
+ B-4 52 75 69 87 69 36 352 O
55
+ B-5 34 50 57 70 48 21 259 O
56
+ B-6 50 69 71 89 65 39 344 X
57
+ B-7 62 78 77 87 76 30 380 O
58
+ B-8 53 82 70 80 68 4 353 O
59
+ B-9 30 64 68 76 53 10 291 O
60
+ B-10 53 78 68 77 66 11 342 O
61
+ B-11 51 69 55 66 67 26 308 O
62
+ B-12 44 65 78 81 71 20 339 O
63
+ B-13 52 81 71 81 73 22 358 O
64
+ B-14 51 76 78 89 70 27 364 O
65
+ B-15 59 71 86 99 79 32 394 O
66
+ B-16 58 78 74 92 74 24 376 O
67
+ B-17 49 70 78 83 71 18 351 O
68
+ B-18 54 73 82 88 83 13 380 O
69
+ B-19 57 79 73 86 71 23 366 O
70
+ B-20 49 68 74 83 67 26 341 O
71
+ B-21 51 75 73 82 75 11 356 O
72
+ B-22 45 65 82 81 75 8 348 O
73
+ B-23 44 59 77 87 66 13 333 O
74
+ B-24 60 82 67 84 79 29 372 O
75
+ B-25 76 97 99 107 88 4 467 O
76
+ B-26 52 67 69 86 65 38 339 X
77
+ B-27 50 53 71 81 73 20 328 X
78
+ B-28 52 81 86 100 73 24 392 O
79
+ B-29 47 74 74 86 66 24 347 O
80
+ B-30 49 66 63 83 60 34 321 O
81
+ B-31 43 62 62 75 54 16 296 O
82
+ B-32 49 75 62 73 62 24 321 O
83
+ B-33 48 65 71 80 61 36 325 X
84
+ B-34 80 98 94 107 97 13 476 O
85
+ B-35 58 75 67 80 78 25 358 O
86
+ B-36 52 74 56 70 68 39 320 O
87
+ B-37 58 63 73 80 79 19 353 O
88
+ B-38 37 71 80 86 58 11 332 O
89
+ B-39 49 63 82 87 69 6 350 O
90
+ B-40 53 75 58 75 63 33 324 X
91
+ B-41 62 84 81 92 72 21 391 O
92
+ B-42 32 63 68 74 50 13 287 O
93
+ B-43 46 62 68 81 68 30 325 X
94
+ B-44 61 76 82 94 79 23 392 O
95
+ B-45 52 69 69 81 66 15 337 X
96
+ B-46 44 79 66 73 63 23 325 X
97
+ B-47 46 79 68 80 61 20 334 X
98
+ B-48 46 83 55 68 48 21 300 O
99
+ B-49 44 68 75 83 67 33 337 O
100
+ B-50 62 80 87 100 88 14 417 O
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dgrep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nishimoto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: grep tsv,csv file
42
+ email:
43
+ - thinkn1108@gmail.com
44
+ executables:
45
+ - dgrep
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/dgrep
56
+ - bin/setup
57
+ - dgrep.gemspec
58
+ - exe/dgrep
59
+ - lib/conf.json
60
+ - lib/dgrep.rb
61
+ - lib/dgrep/version.rb
62
+ - temp.txt
63
+ homepage: https://github.com/nishimoto/dgrep
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.0.14.1
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: grep tsv,csv file
87
+ test_files: []