Class: Masticate::Concat

Inherits:
Object
  • Object
show all
Defined in:
lib/masticate/concat.rb

Overview

concatenate input files:

  • assuming that each input file has a single header line

  • writing a single header line to the output (just use the header line from the first file)

  • trying that all the files have the same format (no validation)

Instance Method Summary collapse

Constructor Details

#initialize(filenames) ⇒ Concat

< Masticate::Base



7
8
9
# File 'lib/masticate/concat.rb', line 7

def initialize(filenames)
  @filenames = filenames
end

Instance Method Details

#concat(opts) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/masticate/concat.rb', line 11

def concat(opts)
  File.unlink(opts[:output]) if opts[:output] && File.exists?(opts[:output])
  redirect = ">>#{opts[:output]}" if opts[:output]
  
  file1, *rest = @filenames
  system "cat #{file1} #{redirect}"
  rest.each do |file|
    system "tail -n +2 #{file} | sed '/^$/d' #{redirect}"
  end
end