Created
December 6, 2012 17:39
-
-
Save ebot/4226393 to your computer and use it in GitHub Desktop.
Reorders a csv file by the specified column.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -wKU | |
require "csv" | |
begin | |
puts "Reordering the index.txt file" | |
# Read in the csv and set it up to be overwritten | |
csv = CSV.read( File.new( '../index.txt', 'r') , | |
{ :headers => true, :col_sep => ';' } ) | |
new = CSV.open( '../index.txt', 'w', { :col_sep => ';' } ) | |
new << csv.headers # Add the header line to the new csv | |
# reorder the hash and write it backout to the csv. | |
csv = csv.sort_by { |col| col['document_modify_date'].to_s } | |
csv.each do |row| | |
new << row | |
end | |
puts "Finished reordering." | |
rescue Exception => e | |
puts "Error: #{e.message}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment