#!/usr/bin/env ruby # -*- coding: utf-8 -*- class Counter FILE_ENCODING = "UTF-8" TARGET_COL = 2 # MiGel CEAN EAN_OUTPUT = 9 def initialize(file) @input = file @result = { :ean_14 => 0, :ean_13 => 0, :output => [], } end def count if @input File.open(@input, "r:#{FILE_ENCODING}") do |input| while line = input.gets row = '' cols = line.split(/,/) if (ean = cols[TARGET_COL] and !ean.empty?) if ean.length > 13 @result[:output] << ean if @result[:ean_14] < EAN_OUTPUT @result[:ean_14] += 1 else @result[:ean_13] += 1 end end end end end end def report puts "14 EAN - #{@result[:ean_14]}" puts "13 EAN - #{@result[:ean_13]}" puts "--------------" puts @result[:output] puts "..." if @result[:ean_14] > @result[:output].length end end unless !ARGV.empty? and file = ARGV.first and File.exist?(file) puts "keine Datei ;(" exit end if File.extname(file) != '.csv' puts "Csv Bitte :-D" exit end counter = Counter.new(file) counter.count counter.report