Bare bones fixed width data processing. Transform fixed width files into CSV or Ruby Hashes.
Add this line to your application's Gemfile:
gem 'fixedwidth'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fixedwidth
Arguments taken are:
- file - path to the fixed width file
- start - a string of start postitions
- stop - a string of stop positions
- header - a string of column names
- delimiter - data delimiter of choice, defaults to comma
Note that the start and stop positions are the column positions in the actual file. The offset is calculated by the Gem so you don't have to do it manually.
Fixedwidth.parse(start: '1,9,17,44,46', stop: '8,16,36,45,63')
Fixedwidth.column_positions # Array of substring positions
# => [[0, 8], [8, 8], [16, 20], [43, 2], [45, 18]]
Example
# ./contacts.txt
# John Smith john@example.com 1-888-555-6666
# Michele O'Reileymichele@example.com 1-333-321-8765
Fixedwidth.parse(file: 'contacts.txt', start: '1,9,17,44,46', stop: '8,16,36,45,63', header: 'first,last,email,blank,phone', delimiter: ",") do |line|
puts line.to_hash # => { first: "John", last: "Smith", email: "john@example.com", blank: "", phone: "1-888-555-6666" }
puts line.to_csv # => "John,Smith,john@example.com,,1-888-555-6666"
# ...rest of loop.
end
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request