cutcsv 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +8 -0
  4. data/README.md +35 -0
  5. data/bin/cutcsv +79 -0
  6. metadata +64 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4706d74dee0e648a8b4983ccbdff662d4f2dd6f6
4
+ data.tar.gz: 5038ed5792575236da3eaa8e7ffe30e0f38ae118
5
+ SHA512:
6
+ metadata.gz: e30719abdfd5159d9f6d4defa97f4af38691562cd7bb86b7ee0008e4d042cc6108055bda89e018188928a75304d00880b816bda8403c07596fec0ae30e26c403
7
+ data.tar.gz: 0c8a925f7c633394cc3394a54570dc766b38acfd2d840b5dd26f20b6e55e757630547ff62a73c982d78cedafbc340dd92fbef5259f691eff4f1f8f62ec85714e
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'main', '~> 5.2.0'
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2013 Rob Miller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # cutcsv
2
+
3
+ CSV is a file format with... issues. Lots of issues. One of the most
4
+ annoying — for me at least — is that it breaks the standard Unix `cut`
5
+ command, which is otherwise a tool that I use every day.
6
+
7
+ If you run `cut` on a CSV and pass in `,` as a delimiter, it often just
8
+ works. But if you've got quoted fields that contain commas, or if you've
9
+ got fields that continue onto multiple lines, things get screwy fast.
10
+
11
+ For the few times that you need to extract fields from such a file,
12
+ `cutcsv` should have your back.
13
+
14
+ ## Installation
15
+
16
+ `cutcsv` can be installed via RubyGems:
17
+
18
+ $ gem install cutcsv
19
+
20
+ ## Usage
21
+
22
+ Like standard `cut`, `cutcsv` accepts input either from standard input or
23
+ from a named list of files. So, you can do:
24
+
25
+ some-command | cutcsv -f 1,3
26
+
27
+ ...or you can do:
28
+
29
+ cutcsv -f 1,3 some-file.csv
30
+
31
+ The `-f` argument is exactly like standard `cut`. You can pass in just
32
+ one field (like `-f 4`), a comma separated list of fields (like `-f
33
+ 1,3,5`), ranges of fields (like `-f 1-10`), or all of these things (like
34
+ `1-3,5,7,9-11,20`).
35
+
data/bin/cutcsv ADDED
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler'
4
+ Bundler.setup
5
+
6
+ require 'main'
7
+
8
+ require 'csv'
9
+
10
+ module Main
11
+ module Cast
12
+ # A custom cast for Main's argument processing. This allows for an
13
+ # argument of, for example:
14
+ #
15
+ # 1,3-5,10,11,15-20
16
+ #
17
+ # to become:
18
+ #
19
+ # [1, 3, 4, 5, 10, 11, 15, 16, 17, 18, 19, 20]
20
+ #
21
+ # ...which allows us to process all the valid arguments to standard
22
+ # Unix cut's -f argument.
23
+ cast :list_of_integers_with_ranges do |obj|
24
+ obj.split(/,|\s+/).map(&:strip).delete_if(&:empty?)
25
+ .flat_map do |value|
26
+ case value
27
+ when /^([0-9]+)-([0-9]+)$/
28
+ Range.new($1.to_i, $2.to_i).to_a
29
+ when /^([0-9]+)$/
30
+ value.to_i
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ Main {
38
+ option('f', 'fields') {
39
+ description 'A comma-separated list of which fields to output. The first column is "1", not "0". So, to output the first, third, and fifth columns, pass in -f1,3,5.'
40
+ argument :required
41
+ cast :list_of_integers_with_ranges
42
+ }
43
+
44
+ argument('files') {
45
+ arity -1
46
+ description 'The files to process. If no file arguments are given, cutcsv will read from standard input.'
47
+ }
48
+
49
+ def run
50
+ inputs = params[:files].values
51
+ inputs = [STDIN] unless inputs.length > 0
52
+
53
+ fields = if params[:fields].given?
54
+ params[:fields].value
55
+ else
56
+ :all
57
+ end
58
+
59
+ inputs.each do |input|
60
+ unless input.respond_to?(:gets)
61
+ input = File.open(input, 'r')
62
+ end
63
+
64
+ CSV.new(input).each do |row|
65
+ csv_row = []
66
+
67
+ row.each_with_index do |column, index|
68
+ field_number = index + 1
69
+
70
+ next unless fields == :all || fields.include?(field_number)
71
+
72
+ csv_row << column
73
+ end
74
+
75
+ puts CSV.generate_line(csv_row)
76
+ end
77
+ end
78
+ end
79
+ }
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cutcsv
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Rob Miller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: main
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ description: A utility that allows you to extract fields from CSV files, outputting
28
+ a new CSV with just the fields that you want.
29
+ email: rob@bigfish.co.uk
30
+ executables:
31
+ - cutcsv
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - bin/cutcsv
36
+ - LICENSE
37
+ - README.md
38
+ - Gemfile
39
+ homepage: https://github.com/robmiller/cutcsv
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.0.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Like Unix cut, but for CSVs
63
+ test_files: []
64
+ has_rdoc: