Skip to content

Instantly share code, notes, and snippets.

@wazoo
Created November 20, 2013 17:06
Show Gist options
  • Save wazoo/7566898 to your computer and use it in GitHub Desktop.
Save wazoo/7566898 to your computer and use it in GitHub Desktop.
Quick script to parse out units in VMware vCOPs CSV Reports
#!/usr/bin/env ruby
# vcops_parse.rb
#
# By: Ben Thomas, Nov 20 2012
#
# MIT License
#
# Removes units from vCOPs CSV report columns, because then you can sort them correctly.
# Turns this:
# Disk Space Total Used
# 1 GB
# 5 GB
#
# In to this:
# Disk Space Total Used (GB)
# 1
# 5
# run like this: ./vcops_parse.rb <filename.csv> && open <filename.csv>_fixed.csv
require 'csv'
csv_in = CSV.read(ARGV[0])
CSV.open("#{ARGV[0]}_fixed.csv", "wb") do |csv|
headers = Array.new
csv_in[0].each_with_index do |col,i|
headers[i] = "#{col} (#{csv_in[1][i].split(' ')[-1]})"
end
csv << headers
csv_in.shift
csv_in.each do |row|
new_row = Array.new
row.each do |col|
case col
when /vCPUs/, /MHz/, /GHz/, /GB/, /MB/, /KB/
col = col.to_f
when '-'
col = "0"
end
new_row << col
end
csv << new_row
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment