gr8 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3f8564628ffbf75fc7bb94b6eda6e9270b143b5
4
+ data.tar.gz: ab024c49b8e07534b5289855fec96ffb5bf68c65
5
+ SHA512:
6
+ metadata.gz: 108d301bd334c933977bc53b97ddf8a225d89a33378374f9647085ba4260a06f8a35832c9f0f57a9081909850922781cf81247bde78ac2588cadade16ae6c467
7
+ data.tar.gz: 72fb0e5ad4ad98ac56c3512335d62b27a643f95ec0d29a68e64b9368ed17e8312b9342def37f7ee19e10c62e12231208f4668aa8b8427425aabd9067d2b74f01
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 kuwata-lab.com all rights reserved.
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,559 @@
1
+ # gr8 README
2
+
3
+ (Release: $Release: 0.1.0 $)
4
+
5
+ gr8 (pronounce as _greight_ or _great_) is a great command-line utility powered by Ruby.
6
+ You can use gr8 instead of sed or awk.
7
+
8
+
9
+ ## Installation
10
+
11
+ $ gem install gr8
12
+
13
+ Or:
14
+
15
+ $ curl -o gr8 http://bit.ly/gr8_rb
16
+ $ chmod a+x gr8
17
+ $ sudo mv gr8 /usr/local/bin
18
+
19
+ Gr8 requires Ruby (>= 2.0).
20
+
21
+
22
+ ## Usage
23
+
24
+ Usage: gr8 _[options]_ ruby-code
25
+
26
+ Options:
27
+
28
+ * -h, --help : show help
29
+ * -v, --version : show version
30
+ * -r lib[,lib2,...] : require libraries
31
+ * -F[regexp] : separate each line with separator
32
+ * -C N : select column (1-origin)
33
+
34
+
35
+ ## Example 1: Aggregation
36
+
37
+ Data file:
38
+
39
+ $ cat data
40
+ Haruhi 100
41
+ Mikuru 80
42
+ Yuki 120
43
+
44
+ Prints each line (`gr8` command prints expression value automatically when non-nil):
45
+
46
+ $ cat data | gr8 '$stdin.lazy.map{|s| s }'
47
+ Haruhi 100
48
+ Mikuru 80
49
+ Yuki 120
50
+
51
+ `$stdin.lazy` is omissible because `gr8` command uses it as current context (=`self`):
52
+
53
+ $ cat data | gr8 'map{|s| s }'
54
+ Haruhi 100
55
+ Mikuru 80
56
+ Yuki 120
57
+
58
+ Select second column:
59
+
60
+ $ cat data | gr8 'map{|s| s.split()[1] }'
61
+ 100
62
+ 80
63
+ 120
64
+
65
+ `map{|s|s.split()}` can be `map{split()}` in `gr8` command
66
+ because `gr` extends `map()` and `select()` to set each item as self
67
+ in block arguments of them:
68
+
69
+ $ cat data | gr8 'map{split()[1]}'
70
+ 100
71
+ 80
72
+ 120
73
+
74
+ Calculates total of numbers:
75
+
76
+ $ cat data | gr8 'map{split()[1]}.map(&:to_i).inject(0,:+)'
77
+ 300
78
+
79
+ `sum()` is a short-hand for `inject(0,:+)`:
80
+
81
+ $ cat data | gr8 'map{split()[1]}.map(&:to_i).sum'
82
+ 300
83
+
84
+ `sum_i()` is a short-hand for `map(&:to_i).inject(0,:+)`:
85
+
86
+ $ cat data | gr8 'map{split()[1]}.sum_i' # or 'sum_f' for float
87
+ 300
88
+
89
+ Command-line opiton '-F' splits each line into array:
90
+
91
+ $ cat data | gr8 -F 'map{self.inspect}'
92
+ ["Haruhi", "100"]
93
+ ["Mikuru", "80"]
94
+ ["Yuki", "120"]
95
+ $ cat data | gr8 -F 'map{self[1]}.sum_i' # or 'map{|a|a[1]}.sum_i'
96
+ 300
97
+
98
+ Command-line option '-C n' selects column (1-origin):
99
+
100
+ $ cat data | gr8 -C 2 'map{self}'
101
+ 100
102
+ 80
103
+ 120
104
+ $ cat data | gr8 -C 2 'sum_i'
105
+ 300
106
+
107
+ Calculates average of numbers instead of total:
108
+
109
+ $ cat data | gr8 -C 2 'map(&:to_i).avg'
110
+ 300.0
111
+ $ cat data | gr8 -C 2 'avg_i'
112
+ 300.0
113
+
114
+ Compared to `ruby -ne`:
115
+
116
+ $ cat data | ruby -ne 'BEGIN{t=0};t+=$_.split[1].to_i;END{p t}'
117
+ 300
118
+ $ cat data | ruby -ane 'BEGIN{t=0};t+=$F[1].to_i;END{p t}'
119
+ 300
120
+
121
+
122
+ ## Example 2: Generating Shell Commands
123
+
124
+ Assume that there are some image files:
125
+
126
+ $ ls
127
+ img1.jpg img2.jpg img3.jpg
128
+ img4.png img5.png img6.png
129
+
130
+ Select PNG files:
131
+
132
+ $ ls | gr8 'grep(/(.*)\.png$/)'
133
+ img1.png
134
+ img2.png
135
+ img3.png
136
+
137
+ Prints new filename replacing '.png' with '.jpg':
138
+
139
+ $ ls | gr8 'grep(/(.*)\.png/) { "#{$1}.jpg" }'
140
+ img1.jpg
141
+ img2.jpg
142
+ img3.jpg
143
+
144
+ Prints OS command to convert PNG file into JPG:
145
+
146
+ $ ls | gr8 'grep(/(.*)\.png/) { "convert #{$1}.png #{$1}.jpg" }'
147
+ convert img1.png img1.jpg
148
+ convert img2.png img2.jpg
149
+ convert img3.png img3.jpg
150
+
151
+ You may want quotes file name with single quotation:
152
+
153
+ $ ls | gr8 'grep(/(.*)\.png/) { "convert #{$1.q}.png #{$1.q}.jpg" }'
154
+ convert 'img1'.png 'img1'.jpg
155
+ convert 'img2'.png 'img2'.jpg
156
+ convert 'img3'.png 'img3'.jpg
157
+
158
+ Or double quotation:
159
+
160
+ $ ls | gr8 'grep(/(.*)\.png/) { "convert #{$1.qq}.png #{$1.qq}.jpg" }'
161
+ convert "img1".png "img1".jpg
162
+ convert "img2".png "img2".jpg
163
+ convert "img3".png "img3".jpg
164
+
165
+ Run os commands after you confirmed them:
166
+
167
+ $ ls | gr8 'grep(/(.*)\.png/){"convert #{$1.q}.png #{$1.q}.jpg"}' | sh
168
+
169
+
170
+ ## Example 3: File Manipulation
171
+
172
+ `Kernel#fu()` is a short-hand which returns `FileUtil` module.
173
+ Using it, You can rename or move files very easily.
174
+
175
+ Assume that there are several PNG files:
176
+
177
+ $ ls | gr8 'grep(/^a(\d+)/)'
178
+ a1.png
179
+ a2.png
180
+ a3.png
181
+
182
+ And you want to rename them to other names:
183
+
184
+ $ ls | gr8 'grep(/^a(\d+)/) { "b#{$1.to_i+100}.png" }'
185
+ b101.png
186
+ b102.png
187
+ b103.png
188
+
189
+ `fu.mv` is a short-hand to `require "fileutils"; FileUtils.mv`:
190
+
191
+ $ ls | gr8 'grep(/^a(\d+)/){fu.mv "a#{$1}.png", "b#{$1.to_i+100}.png"}'
192
+ $ ls b*.png
193
+ b101.png b102.png b103.png # renamed from 'a1.png', 'a2.png' and 'a3.png'
194
+
195
+ (Experimental)
196
+
197
+ `gr8` provides more convenient methods to mapipulate files:
198
+
199
+ $ ls | gr8 'copy_as { sub(/\.htm$/, ".html") }'
200
+ $ ls | gr8 'copy_as! { sub(/\.htm$/, ".html") }' # overwrite existing file
201
+ $ ls | gr8 'rename_as { sub(/\.htm$/, ".html") }'
202
+ $ ls | gr8 'rename_as! { sub(/\.htm$/, ".html") }' # overwrite existing file
203
+ $ ls | gr8 'copy_to { "some/where/directory/" }'
204
+ $ ls | gr8 'copy_to! { "some/where/directory/" }' # overwrite existing file
205
+ $ ls | gr8 'move_to { "some/where/directory/" }'
206
+ $ ls | gr8 'move_to! { "some/where/directory/" }' # overwrite existing file
207
+
208
+
209
+ ## References
210
+
211
+
212
+ ### Kernel#fu()
213
+
214
+ Returns `FileUtils` class object.
215
+
216
+ Example:
217
+
218
+ $ ls | gr8 'grep(/(.*)\.png/){fu.mv "#{$1}.png", "#{$1}.jpg"}'
219
+
220
+
221
+ ### String#q(), #qq()
222
+
223
+ `q()` quotes string with single-quotation, with escaping singile-quotation with backslash.
224
+
225
+ `qq()` quotes string with double-quotation, with escaping singile-quotation with backslash.
226
+
227
+ These are convenient when file name contains spaces.
228
+
229
+ Example:
230
+
231
+ $ echo 'Image 1.png' | gr8 'grep(/(.*)\.png/){"convert #{$1.q}.png #{$1.q}.jpg"}'
232
+ convert 'Image 1'.png 'Image 1'.jpg
233
+ $ ls | gr8 'grep(/(.*)\.png/){"convert #{$1.qq}.png #{$1.qq}.jpg"}'
234
+ convert "Image 1".png "Image 1".jpg
235
+
236
+
237
+ ### Enumerable#transform(){...}, #xf(){...}
238
+
239
+ Similar to `map()`, but it sets each item as self in block argument.
240
+
241
+ Example:
242
+
243
+ $ ls *.png | gr8 'xf{self}'
244
+ A.png
245
+ B.png
246
+ C.png
247
+ $ ls *.png | gr8 'xf{sub(/\.png/, '.jpg')}'
248
+ A.jpg
249
+ B.jpg
250
+ C.jpg
251
+
252
+ Source code:
253
+
254
+ def transform(&block)
255
+ collect {|x| x.instance_exec(x, &block) }
256
+ end
257
+ alias xf transform
258
+
259
+
260
+ ### Enumerable#map(){...}
261
+
262
+ Extended to set each item as self in block argument of `map()`.
263
+ If you want original `map()`, use `collect()` instead.
264
+
265
+ Example:
266
+
267
+ $ ls *.png | gr8 'map{self}'
268
+ A.png
269
+ B.png
270
+ C.png
271
+ $ ls *.png | gr8 'map{sub(/\.png/, '.jpg')}'
272
+ A.jpg
273
+ B.jpg
274
+ C.jpg
275
+
276
+ Source code:
277
+
278
+ alias __map map
279
+ def map(&block)
280
+ __map {|x| x.instance_exec(x, &block) }
281
+ end
282
+
283
+
284
+ ### Enumerable#select(){...}
285
+
286
+ Extended to set each item as self in block argument of `select()`.
287
+ If you want original `select()`, use `find_all()` instead.
288
+
289
+ Example:
290
+
291
+ $ ls *.png | gr8 'select{self}'
292
+ A.png
293
+ B.png
294
+ C.png
295
+ $ ls *.png | gr8 'select{start_with?("B")}'
296
+ B.jpg
297
+
298
+ Source code:
299
+
300
+ alias __select select
301
+ def select(&block)
302
+ __select {|x| x.instance_exec(x, &block) }
303
+ end
304
+
305
+
306
+ ### Enumerable#sum()
307
+
308
+ Same as `inject(0, :+)`.
309
+
310
+ Example:
311
+
312
+ $ cat file
313
+ 10.5
314
+ 20.5
315
+ 30.5
316
+ $ cat file | gr8 'map(&:to_f).sum'
317
+ 61.5
318
+
319
+
320
+ ### Enumerable#sum_i(), #sum_f()
321
+
322
+ Same as `map(&:to_i).inject(0, :+)` or `map(&:to_f).inject(0, :+)`
323
+
324
+ Example:
325
+
326
+ $ cat file
327
+ 10.5
328
+ 20.5
329
+ 30.5
330
+ $ cat file | gr8 'sum_i'
331
+ 60
332
+ $ cat file | gr8 'sum_f'
333
+ 61.5
334
+
335
+
336
+ ### Enumerable#avg()
337
+
338
+ Returns average of numbers.
339
+
340
+ Example:
341
+
342
+ $ cat file
343
+ 10.1
344
+ 20.2
345
+ 30.3
346
+ $ cat data |gr8 'map(&:to_i).avg'
347
+ 20.0
348
+
349
+
350
+ ### Enumerable#avg_i(), #avg_f()
351
+
352
+ Same as `map(&:to_i).avg` or `map(&:to_f).avg`.
353
+
354
+ Example:
355
+
356
+ $ cat file
357
+ 10.1
358
+ 20.2
359
+ 30.3
360
+ $ cat data |gr8 'avg_i'
361
+ 20.0
362
+ $ cat data |gr8 'avg_f'
363
+ 20.2
364
+
365
+
366
+ ### Enumerable#sed(pattern, replacing), #sed(pattern){...}
367
+
368
+ Replaces the first pattern in each line with replacing string or block.
369
+ Internally, `sed()` calls `String#sub()`.
370
+
371
+ Example:
372
+
373
+ $ ls *.png
374
+ A.png
375
+ B.png
376
+ C.png
377
+ $ ls *.png | gr8 'sed(/png/, "jpg")'
378
+ A.png
379
+ B.png
380
+ C.png
381
+
382
+ Source code:
383
+
384
+ def sed(pat, str=nil, &block)
385
+ if block_given?
386
+ collect {|s| s.sub(pat, &block) }
387
+ else
388
+ collect {|s| s.sub(pat, str) }
389
+ end
390
+ end
391
+
392
+
393
+ ### Enumerable#gsed(pattern, replacing), #gsed(pattern){...}
394
+
395
+ Replaces all of pattern in each line with replacing string or block.
396
+ Internally, `gsed()` calls `String#gsub()`.
397
+
398
+ Example:
399
+
400
+ $ ls *.png
401
+ A1-1.png
402
+ A1-2.png
403
+ A1-3.png
404
+ $ ls *.png | gr8 'sed(/\d+/, "00\\&")'
405
+ A001-1.png
406
+ A001-2.png
407
+ A001-3.png
408
+ $ ls *.png | gr8 'gsed(/\d+/, "00\\&")'
409
+ A001-001.png
410
+ A001-002.png
411
+ A001-003.png
412
+
413
+ Source code:
414
+
415
+ def gsed(pat, str=nil, &block)
416
+ if block_given?
417
+ collect {|s| s.gsub(pat, &block) }
418
+ else
419
+ collect {|s| s.gsub(pat, str) }
420
+ end
421
+ end
422
+
423
+
424
+ ### Enumerable#paths(), #paths{...}
425
+
426
+ Converts each item into `Pathname` object.
427
+ Library `pathname` will be loaded automatically.
428
+
429
+ Example:
430
+
431
+ $ /bin/ls | gr8 'paths{|x| "#{x}: #{x.ftype}"}'
432
+ MIT-LICENSE: file
433
+ README.txt: file
434
+ Rakefile: file
435
+ bin: directory
436
+ lib: directory
437
+ test: directory
438
+
439
+ Source code:
440
+
441
+ def paths(&block)
442
+ require "pathname" unless defined?(Pathname)
443
+ if block_given?
444
+ collect {|s| x = Pathname(s); x.instance_exec(x, &block) }
445
+ else
446
+ collect {|s| Pathname(s) }
447
+ end
448
+ end
449
+
450
+
451
+ ### Enumerable#edit(verbose=true, encodint='utf-8'){|content, filepath| ...}
452
+
453
+ Replace file content with result of block argument.
454
+
455
+ Example:
456
+
457
+ $ ls *.rb | gr8 'edit{|s|
458
+ s = s.gsub(/Release: \d+\.\d+\.\d+/, "Release: 1.2.3")
459
+ s = s.gsub(/Copyright: \d+-\d+/, "Copyright: 2013-2015")
460
+ s }'
461
+
462
+
463
+ ### Enumerable#edit_i(suffix, verbose=true, encodint='utf-8'){|content, filepath| ...}
464
+
465
+ Copy backup file with suffix before editing file.
466
+
467
+ Example:
468
+
469
+ $ ls *.rb
470
+ hom.rb mad.rb
471
+ $ ls *.rb | gr8 'edit_i(".bkup"){|s|
472
+ s = s.gsub(/Release: \d+\.\d+\.\d+/, "Release: 1.2.3")
473
+ s = s.gsub(/Copyright: \d+-\d+/, "Copyright: 2013-2015")
474
+ s }'
475
+ $ ls *.bkup
476
+ hom.rb hom.rb.bkup mad.rb mad.rb.bkup
477
+
478
+
479
+ ### Enumerable#copy_to{...}, #copy_to!{...}
480
+
481
+ (Experimental)
482
+
483
+ Copy files into destination directory, without renaming basename.
484
+
485
+ * Block argument should return destination directory name.
486
+ * `copy_to()` skips when destination file already exists.
487
+ * `copy_to!()` overwrites when destination file already exists.
488
+ * Both skips copying when destination directory doesn't exist.
489
+
490
+
491
+ ### Enumerable#mkdir_and_copy_to{...}, #mkdir_and_copy_to!{...}
492
+
493
+ (Experimental)
494
+
495
+ Similar to `copy_to()` or `copy_to!()` except creating destination directory when not exist.
496
+
497
+
498
+ ### Enumerable#move_to{...}, #move_to!{...}
499
+
500
+ (Experimental)
501
+
502
+ Move files into destination directory, without renaming basename.
503
+
504
+ * Block argument should return destination directory name.
505
+ * `move_to()` skips when destination file already exists.
506
+ * `move_to!()` overwrites when destination file already exists.
507
+ * Both skips moving files when destination directory doesn't exist.
508
+
509
+
510
+ ### Enumerable#mkdir_and_move_to{...}, #mkdir_and_move_to!{...}
511
+
512
+ (Experimental)
513
+
514
+ Similar to `move_to()` or `move_to!()` except creating destination directory when not exist.
515
+
516
+
517
+ ### Enumerable#copy_as{...}, #copy_as!{...}
518
+
519
+ (Experimental)
520
+
521
+ Copy files into destination directory, with renaming basename.
522
+
523
+ * Block argument should return destination file name.
524
+ * `copy_as()` skips when destination file already exists.
525
+ * `copy_as!()` overwrites when destination file already exists.
526
+ * Both skips copying when destination directory doesn't exist.
527
+
528
+
529
+ ### Enumerable#mkdir_and_copy_as{...}, #mkdir_and_copy_as!{...}
530
+
531
+ (Experimental)
532
+
533
+ Similar to `copy_as()` or `copy_as!()` except creating destination directory when not exist.
534
+
535
+
536
+ ### Enumerable#rename_as{...}, #rename_as!{...}
537
+
538
+ (Experimental)
539
+
540
+ Move files into destination directory, with renaming basename.
541
+
542
+ * Block argument should return destination file name.
543
+ * `rename_as()` skips when destination file already exists.
544
+ * `rename_as!()` overwrites when destination file already exists.
545
+ * Both skips moving when destination directory doesn't exist.
546
+
547
+
548
+ ### Enumerable#mkdir_and_rename_as{...}, #mkdir_and_rename_as!{...}
549
+
550
+ (Experimental)
551
+
552
+ Similar to `rename_as()` or `rename_as!()` except creating destination directory when not exist.
553
+
554
+
555
+ ## License and Copyright
556
+
557
+ $License: MIT License $
558
+
559
+ $Copyright: copyright(c) 2015 kuwata-lab.com all rights reserved $