cmdtabs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 112a3fc1f558b0175f2d2905444c7a139204a600e2ba59d096fcbe616d48f0e5
4
+ data.tar.gz: be9484e39d920387ed525ae22efa8bf74ba8c176bcd3fa85cd3094557c320c6d
5
+ SHA512:
6
+ metadata.gz: 0a3ac38dde1c8675a225674edae8a4c580928ede5cb68c97fd218a36bf1592390fe8b29dbddfae08bd7343f6aad77149bd21eab0d2f7dfdb28ca1a0b0bc31455
7
+ data.tar.gz: e537e117d62911b7bb43025e9952ee1ff0f3a7a9af868471fb95351cd463206926be2a97d24e00aa922ad06a55d5a812173e9b752055e9690606c8e426372b8f
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at seoanezonjic@hotmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cmdtabs.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 seoanezonjic
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,43 @@
1
+ # Cmdtabs
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cmdtabs`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cmdtabs'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cmdtabs
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cmdtabs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/cmdtabs/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Cmdtabs project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cmdtabs/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,56 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ options = {}
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
8
+
9
+ options[:input] = nil
10
+ opts.on("-i", "--input_file PATH", "Path to input file") do |item|
11
+ options[:input] = item
12
+ end
13
+
14
+ options[:col_index] = nil
15
+ opts.on("-x", "--column_index INTEGER", "Column index (0 based) to use as reference") do |item|
16
+ options[:col_index] = item.to_i
17
+ end
18
+
19
+ options[:sep] = ','
20
+ opts.on("-s", "--separator STRING", "Character separator when collapse data") do |item|
21
+ options[:sep] = item
22
+ end
23
+
24
+ options[:col_aggregate] = nil
25
+ opts.on("-a", "--column_aggregate INTEGER", "Column index (0 based) to extract data and join for each id in column index") do |item|
26
+ options[:col_aggregate] = item.to_i
27
+ end
28
+
29
+ opts.on_tail("-h", "--help", "Show this message") do
30
+ puts opts
31
+ exit
32
+ end
33
+ end.parse!
34
+
35
+
36
+ agg_data = {}
37
+ if options[:input] == '-'
38
+ input = STDIN
39
+ else
40
+ input = File.open(options[:input])
41
+ end
42
+ input.each do |line|
43
+ fields = line.chomp.split("\t")
44
+ key = fields[options[:col_index]]
45
+ val = fields[options[:col_aggregate]]
46
+ query = agg_data[key]
47
+ if query.nil?
48
+ agg_data[key] = [val]
49
+ else
50
+ query << val
51
+ end
52
+ end
53
+
54
+ agg_data.each do |key, values|
55
+ STDOUT.puts "#{key}\t#{values.join(options[:sep])}"
56
+ end
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ metric_file = ARGV[0]
4
+ fixCols = ARGV[1].split(',')
5
+ output = ARGV[2]
6
+ name_tag = fixCols.shift
7
+ fixColNumber = fixCols.length
8
+
9
+ hash = {}
10
+
11
+ varTags = []
12
+ File.open(ARGV[0]).each do |line|
13
+ line.chomp!
14
+ fields = line.split("\t")
15
+ name = fields.shift
16
+ fixFields = fields[0..fixColNumber-1]
17
+ varFields = fields[fixColNumber..fixColNumber+1]
18
+ varTags << varFields.first if !varTags.include?(varFields.first)
19
+
20
+ query = hash[name]
21
+ if query.nil?
22
+ hash[name] = {varFields.first => varFields.last}
23
+ fixCols.each_with_index do |tag, i|
24
+ hash[name][tag] = fixFields[i]
25
+ end
26
+ else
27
+ query[varFields.first] = varFields.last
28
+ end
29
+ end
30
+
31
+ metric_table = File.new(output, "w")
32
+ if fixColNumber > 0
33
+ header="#{name_tag}\t#{fixCols.join("\t")}\t#{varTags.join("\t")}"
34
+ else
35
+ header="#{name_tag}\t#{varTags.join("\t")}"
36
+ end
37
+
38
+ metric_table.puts(header)
39
+ allTags = fixCols.concat(varTags)
40
+ hash.each do |name, fields|
41
+ array_temp = [name]
42
+ allTags.each do |tag|
43
+ array_temp << fields[tag]
44
+ end
45
+ metric_table.puts(array_temp.join("\t"))
46
+ end
47
+ metric_table.close()
48
+
49
+
@@ -0,0 +1,46 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ options = {}
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: #{__FILE__} [options]"
8
+
9
+ options[:input] = nil
10
+ opts.on("-i", "--input_file PATH", "Path to input file") do |item|
11
+ options[:input] = item
12
+ end
13
+
14
+ options[:col_index] = nil
15
+ opts.on("-x", "--column_index INTEGER", "Column index (0 based) to use as reference") do |item|
16
+ options[:col_index] = item.to_i
17
+ end
18
+
19
+ options[:sep] = ","
20
+ opts.on("-s", "--sep_char STRING", "Field character delimiter") do |item|
21
+ options[:sep] = item
22
+ end
23
+
24
+ opts.on_tail("-h", "--help", "Show this message") do
25
+ puts opts
26
+ exit
27
+ end
28
+ end.parse!
29
+
30
+
31
+ agg_data = {}
32
+ if options[:input] == '-'
33
+ input = STDIN
34
+ else
35
+ input = File.open(options[:input])
36
+ end
37
+ input.each do |line|
38
+ fields = line.chomp.split("\t")
39
+ target_field = fields[options[:col_index]]
40
+ target_field.split(options[:sep]).each do |val|
41
+ record = fields[0..(options[:col_index]-1)] + [val] + fields[(options[:col_index] + 1)..fields.length]
42
+ #record = fields[0..(options[:col_index] + 1)] + [val] + fields[(options[:col_index] + 1)..fields.length]
43
+ STDOUT.puts record.join("\t")
44
+ end
45
+ end
46
+
@@ -0,0 +1,93 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ #####################################################################
6
+ ## METHODS
7
+ ######################################################################
8
+
9
+ def load_records(file, cols, sep)
10
+ recs = {}
11
+ File.open(file).each do |line|
12
+ fields = line.chomp.split(sep)
13
+ recs[cols.map{|c| fields[c]}] = true
14
+ end
15
+ return recs.keys
16
+ end
17
+
18
+ def print_recs(recs, sep)
19
+ recs.each do |rec|
20
+ puts rec.join(sep)
21
+ end
22
+ end
23
+
24
+ #####################################################################
25
+ ## OPTPARSE
26
+ ######################################################################
27
+
28
+ options = {}
29
+ OptionParser.new do |opts|
30
+ opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
31
+
32
+ options[:a_file] = nil
33
+ opts.on("-a", "--a_file PATH", "Path to input file") do |item|
34
+ options[:a_file] = item
35
+ end
36
+
37
+ options[:b_file] = nil
38
+ opts.on("-b", "--a_file PATH", "Path to input file") do |item|
39
+ options[:b_file] = item
40
+ end
41
+
42
+ options[:a_cols] = [0]
43
+ opts.on("-A", "--a_cols STRING", "Index of columns in base 0 to compare") do |item|
44
+ options[:a_cols] = item.split(',').map{|n| n.to_i}
45
+ end
46
+
47
+ options[:b_cols] = [0]
48
+ opts.on("-B", "--b_cols STRING", "Index of columns in base 0 to compare") do |item|
49
+ options[:b_cols] = item.split(',').map{|n| n.to_i}
50
+ end
51
+
52
+ options[:count] = false
53
+ opts.on("-c", "--count", "Only compute number of matches") do
54
+ options[:count] = true
55
+ end
56
+
57
+ options[:keep] = 'c'
58
+ opts.on("-k", "--keep STRING", "Keep records. c for common, 'a' for specific of file a, 'b' for specific of file b and 'ab' for specific of file a AND b") do |item|
59
+ options[:keep] = item
60
+ end
61
+
62
+ options[:sep] = "\t"
63
+ opts.on("-s", "--separator STRING", "column character separator") do |item|
64
+ options[:sep] = item
65
+ end
66
+
67
+ opts.on_tail("-h", "--help", "Show this message") do
68
+ puts opts
69
+ exit
70
+ end
71
+ end.parse!
72
+
73
+ a_records = load_records(options[:a_file], options[:a_cols], options[:sep])
74
+ b_records = load_records(options[:b_file], options[:b_cols], options[:sep])
75
+
76
+ common = a_records & b_records
77
+ a_only = a_records - common
78
+ b_only = b_records - common
79
+ if options[:count]
80
+ puts "a: #{a_only.length}"
81
+ puts "b: #{b_only.length}"
82
+ puts "c: #{common.length}"
83
+ else
84
+ if options[:keep] == 'c'
85
+ print_recs(common, options[:sep])
86
+ elsif options[:keep] == 'a'
87
+ print_recs(a_only, options[:sep])
88
+ elsif options[:keep] == 'b'
89
+ print_recs(b_only, options[:sep])
90
+ elsif options[:keep] == 'ab'
91
+ print_recs(a_only + b_only, options[:sep])
92
+ end
93
+ end
@@ -0,0 +1,41 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ parent_table = {}
4
+ table_length = 0
5
+
6
+ ARGV.each do |file_name|
7
+
8
+ local_length = 0
9
+ File.open(file_name).each do |line|
10
+ line.chomp!
11
+ n_fields = line.count("\t")+1
12
+ fields = line.split("\t", n_fields).map{|field|
13
+ if field == ""
14
+ '-'
15
+ else
16
+ field
17
+ end
18
+ }
19
+ next if fields.count('-') == fields.length #skip blank records
20
+ id = fields.shift
21
+ local_length = fields.length
22
+ if !parent_table.has_key?(id)
23
+ parent_table[id] = Array.new(table_length,'-')
24
+ elsif parent_table[id].length < table_length
25
+ parent_table[id].concat(Array.new(table_length-parent_table[id].length,'-'))
26
+ end
27
+ parent_table[id].concat(fields)
28
+
29
+ end
30
+
31
+ table_length += local_length
32
+ parent_table.each do |id, fields|
33
+ diference = table_length - fields.length
34
+ fields.concat(Array.new(diference,'-')) if diference > 0
35
+ end
36
+
37
+ end
38
+
39
+ parent_table.each do |id, fields|
40
+ puts id+"\t"+fields.join("\t")
41
+ end
@@ -0,0 +1,59 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ options = {}
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: #{__FILE__} [options]"
8
+
9
+ options[:input_file] = nil
10
+ opts.on("-i", "--input_file PATH", "Input file ") do |input_file|
11
+ options[:input_file] = input_file
12
+ end
13
+
14
+ options[:index_file] = nil
15
+ opts.on("-I", "--index_file PATH", "Index file ") do |item|
16
+ options[:index_file] = item
17
+ end
18
+
19
+ options[:input_separator] = "\t"
20
+ opts.on("-s", "--input_separator STRING", "Separator character") do |item|
21
+ options[:input_separator] = item
22
+ end
23
+
24
+ options[:columns] = [1]
25
+ opts.on("-c", "--columns STRING", "Columns indexes, comma separated, to perform the ID translations.") do |item|
26
+ options[:columns] = item.split(','). map{|i| i.to_i - 1}
27
+ end
28
+
29
+ options[:from] = 0
30
+ opts.on("-f", "--from INTEGER", "Column in index file to take reference value. Default 1. Numeration is 1 based") do |item|
31
+ options[:from] = item.to_i - 1
32
+ end
33
+
34
+ options[:to] = 1
35
+ opts.on("-t", "--to INTEGER", "Column in index file to take the value that will be used in substitution. Default 2. Numeration is 1 based") do |item|
36
+ options[:to] = item.to_i - 1
37
+ end
38
+
39
+ end.parse!
40
+
41
+ #Load index
42
+ index = {}
43
+ File.open(options[:index_file]).read.each_line do |line|
44
+ line.chomp!
45
+ fields = line.split("\t")
46
+ index[fields[options[:from]]] = fields[options[:to]]
47
+ end
48
+
49
+ #Reemplaza nombres
50
+ File.open(options[:input_file]+'_rep','w') do |f|
51
+ File.open(options[:input_file]).each do |line|
52
+ fields = line.chomp.split(options[:input_separator])
53
+ options[:columns].each do |col|
54
+ new_string = index[fields[col]]
55
+ fields[col] = new_string if !new_string.nil?
56
+ end
57
+ f.puts fields.join(options[:input_separator])
58
+ end
59
+ end
@@ -0,0 +1,207 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'find'
4
+ require 'optparse'
5
+
6
+ #################################################################################################
7
+ ## FUNCTIONS
8
+ #################################################################################################
9
+ def parse_cols(col_string)
10
+ cols = col_string.split(',').map{|col| col.to_i}
11
+ return cols
12
+ end
13
+
14
+ def build_pattern(col_filter, keywords)
15
+ pattern = {}
16
+ if col_filter.nil? || keywords.nil?
17
+ else
18
+ keys_per_col = keywords.split('%')
19
+ if keys_per_col.length != col_filter.length
20
+ puts 'Number of keywords not equal to number of filtering columns'
21
+ Process.exit
22
+ end
23
+ col_filter.each_with_index do |col, i|
24
+ pattern[col] = keys_per_col[i].split('&')
25
+ end
26
+ end
27
+ return pattern
28
+ end
29
+
30
+ def match(string, key, match_mode)
31
+ match = false
32
+ if string.nil?
33
+ match = false
34
+ elsif match_mode == 'i'
35
+ match = string.include?(key)
36
+ elsif match_mode == 'c'
37
+ if string == key
38
+ match = true
39
+ end
40
+ end
41
+ return match
42
+ end
43
+
44
+ def filter(header, pattern, search_mode, match_mode, reverse = false)
45
+ filter = false
46
+ pattern.each do |col,keys|
47
+ match = false
48
+ keys.each do |key|
49
+ if match(header[col], key, match_mode)
50
+ match =true
51
+ end
52
+ end
53
+ if match
54
+ if search_mode == 's'
55
+ filter = false
56
+ break
57
+ end
58
+ elsif !match && search_mode == 'c'
59
+ filter = true
60
+ break
61
+ elsif !match
62
+ filter = true
63
+ end
64
+ end
65
+ if reverse
66
+ filter = !filter
67
+ end
68
+ return filter
69
+ end
70
+
71
+ def check_file(file, names, options, pattern)
72
+ if file == '-'
73
+ input = STDIN
74
+ else
75
+ input = File.open(file)
76
+ end
77
+ relations = relations(options[:column])
78
+ input.read.each_line do |line|
79
+ line.chomp!
80
+ header = line.split(options[:separator])
81
+ if pattern.nil? || !filter(header, pattern, options[:search_mode], options[:match_mode], options[:reverse])
82
+ options[:column].each do |col|
83
+ if !options[:check_uniq] || !names[relations[col]].include?(header[col])
84
+ names[relations[col]] << header[col]
85
+ end
86
+ end
87
+ end
88
+ end
89
+ return names
90
+ end
91
+
92
+ def relations(column)
93
+ relations = {}
94
+ column.each_with_index do |col,i|
95
+ relations[col] = i
96
+ end
97
+ return relations
98
+ end
99
+
100
+ def report(names)
101
+ n_col = names.length
102
+ names.first.length.times do |y|
103
+ n_col.times do |x|
104
+ string = "#{names[x][y]}"
105
+ if x < n_col-1
106
+ string << "\t"
107
+ end
108
+ print string
109
+ end
110
+ puts
111
+ end
112
+ end
113
+
114
+ #################################################################################################
115
+ ## INPUT PARSING
116
+ #################################################################################################
117
+ options = {}
118
+
119
+ optparse = OptionParser.new do |opts|
120
+ options[:table_file] = nil
121
+ opts.on( '-t', '--table_file FILE', 'Input tabulated file' ) do |table_file|
122
+ options[:table_file] = table_file
123
+ end
124
+
125
+ options[:column] = [0]
126
+ opts.on( '-c', '--column STRING', 'Column/s to show. Format: x,y,z..' ) do |column|
127
+ options[:column] = parse_cols(column)
128
+ end
129
+
130
+ options[:col_filter] = nil
131
+ opts.on( '-f', '--col_filter STRING', 'Select columns where search keywords. Format: x,y,z..' ) do |col_filter|
132
+ options[:col_filter] = parse_cols(col_filter)
133
+ end
134
+
135
+ options[:keywords] = nil
136
+ opts.on( '-k', '--keywords STRING', 'Keywords for select rows. Format: key1_col1&key2_col1%key1_col2&key2_col2' ) do |keywords|
137
+ options[:keywords] = keywords
138
+ end
139
+
140
+ options[:search_mode] = 'c'
141
+ opts.on( '-s', '--search STRING', 'c a match per column, s some match in some column. Default c' ) do |search_mode|
142
+ options[:search_mode] = search_mode
143
+ end
144
+
145
+ options[:match_mode] = 'i'
146
+ opts.on( '-m', '--match_mode STRING', 'i string must include the keyword, c for fullmatch. Default i') do |match_mode|
147
+ options[:match_mode] = match_mode
148
+ end
149
+
150
+ options[:separator] = "\t"
151
+ opts.on( '-p', '--separator STRING', 'Separator used in fields. Default i') do |separator|
152
+ options[:separator] = separator
153
+ end
154
+
155
+ options[:reverse] = false
156
+ opts.on( '-r', '--reverse', 'Select not matching' ) do
157
+ options[:reverse] = true
158
+ end
159
+
160
+ options[:uniq] = false
161
+ opts.on( '-u', '--uniq', 'Delete redundant items' ) do
162
+ options[:uniq] = true
163
+ end
164
+
165
+ # Set a banner, displayed at the top of the help screen.
166
+ opts.banner = "Usage: table_header.rb -t tabulated_file \n\n"
167
+
168
+ # This displays the help screen
169
+ opts.on( '-h', '--help', 'Display this screen' ) do
170
+ puts opts
171
+ exit
172
+ end
173
+
174
+ end # End opts
175
+
176
+ # parse options and remove from ARGV
177
+ optparse.parse!
178
+
179
+ ##################################################################################################
180
+ ## MAIN
181
+ ##################################################################################################
182
+ if options[:table_file].nil?
183
+ puts 'Tabulated file not specified'
184
+ Process.exit
185
+ end
186
+
187
+ pattern = build_pattern(options[:col_filter], options[:keywords])
188
+
189
+ names = []
190
+ options[:column].length.times do
191
+ names << []
192
+ end
193
+ if options[:table_file].include?('*')
194
+ Find.find(Dir.pwd) do |path|
195
+ if FileTest.directory?(path)
196
+ next
197
+ else
198
+ if File.basename(path) =~ /#{options[:table_file]}/
199
+ names = check_file(path, names, options, pattern)
200
+ end
201
+ end
202
+ end
203
+ else
204
+ names = check_file(options[:table_file], names, options, pattern)
205
+ end
206
+
207
+ report(names)
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ # Pedro Seoane Zonjic 13-12-2012
3
+ # Toma la informacion extraida de un archivo tabulado (donde la primera columna es el idetificador) en base a una lista de identificadores proporcionada
4
+ # la informacion se guarda en el archivo de salida
5
+
6
+ if ARGV.size < 3
7
+ puts "Usage: table_linker.rb file_table file_table output_file_name"
8
+ Process.exit
9
+ end
10
+
11
+ drop_line = false
12
+ if !ARGV[3].nil?
13
+ drop_line = true
14
+ end
15
+ hash_info={}
16
+
17
+ #Cargar tabla de informacion en hash en forma {identificador => campos de informacion}
18
+ File.open(ARGV[0],'r').each do |line|
19
+ fields=line.chomp.split("\t",2)
20
+ hash_info[fields.first]=fields.last
21
+ end
22
+
23
+ save_info=File.open(ARGV[2],'w') #Crea archivo para guardar la informacion
24
+ File.open(ARGV[1],'r').each do |line|
25
+ line.chomp!
26
+ fields = line.split("\t")
27
+ id = fields.first
28
+ info_id=hash_info[id]
29
+ if !info_id.nil?
30
+ save_info.puts line+"\t"+info_id
31
+ else
32
+ save_info.puts line if !drop_line
33
+ end
34
+ end
35
+ save_info.close
data/cmdtabs.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/cmdtabs/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cmdtabs"
7
+ spec.version = Cmdtabs::VERSION
8
+ spec.authors = ["seoanezonjic"]
9
+ spec.email = ["seoanezonjic@hotmail.com"]
10
+
11
+ spec.summary = "Gem to manipulate text tables in cmd"
12
+ spec.description = "Toolset to merge, colapse tables rename field contents, etc "
13
+ spec.homepage = "https://github.com/seoanezonjic/cmdtabs"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "bin"
29
+ spec.executables = spec.files.grep(%r{\Abin/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cmdtabs
4
+ VERSION = "0.1.0"
5
+ end
data/lib/cmdtabs.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cmdtabs/version"
4
+
5
+ module Cmdtabs
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmdtabs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - seoanezonjic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-03-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Toolset to merge, colapse tables rename field contents, etc '
14
+ email:
15
+ - seoanezonjic@hotmail.com
16
+ executables:
17
+ - aggregate_column_data.rb
18
+ - create_metric_table.rb
19
+ - desaggregate_column_data.rb
20
+ - intersect_columns.rb
21
+ - merge_tabular.rb
22
+ - standard_name_replacer.rb
23
+ - table_header.rb
24
+ - table_linker.rb
25
+ extensions: []
26
+ extra_rdoc_files: []
27
+ files:
28
+ - ".github/workflows/main.yml"
29
+ - ".gitignore"
30
+ - ".rspec"
31
+ - ".rubocop.yml"
32
+ - CODE_OF_CONDUCT.md
33
+ - Gemfile
34
+ - LICENSE.txt
35
+ - README.md
36
+ - Rakefile
37
+ - bin/aggregate_column_data.rb
38
+ - bin/create_metric_table.rb
39
+ - bin/desaggregate_column_data.rb
40
+ - bin/intersect_columns.rb
41
+ - bin/merge_tabular.rb
42
+ - bin/standard_name_replacer.rb
43
+ - bin/table_header.rb
44
+ - bin/table_linker.rb
45
+ - cmdtabs.gemspec
46
+ - lib/cmdtabs.rb
47
+ - lib/cmdtabs/version.rb
48
+ homepage: https://github.com/seoanezonjic/cmdtabs
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/seoanezonjic/cmdtabs
53
+ source_code_uri: https://github.com/seoanezonjic/cmdtabs
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.4.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.2.15
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Gem to manipulate text tables in cmd
73
+ test_files: []