Skip to content

Instantly share code, notes, and snippets.

@ebot
Created December 6, 2012 17:39
Show Gist options
  • Save ebot/4226393 to your computer and use it in GitHub Desktop.
Save ebot/4226393 to your computer and use it in GitHub Desktop.
Reorders a csv file by the specified column.
#!/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