Skip to content

Instantly share code, notes, and snippets.

@geezyx
Created January 11, 2019 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geezyx/ec361c723f080b6ac5c0f48e2dfccd33 to your computer and use it in GitHub Desktop.
Save geezyx/ec361c723f080b6ac5c0f48e2dfccd33 to your computer and use it in GitHub Desktop.
Script to take AWS right sizing output and parse out tags into CSV columns, for easier interpretation
require "csv"
inputarray = ARGV
if inputarray.length != 2
puts "usage: ruby tagcolumns.rb <input filename> <output filename>"
exit
end
a = CSV.read(ARGV[0])
all_tags = Hash.new
csv_columns = a[0].length
a[1..a.length-2].each do |row|
begin
kvhash = Hash.new("")
kvpairs = row[row.length-1].split(" | ")
kvpairs.each do |kv|
k,v = kv.split(":")
kvhash[k] = v
all_tags[k] = ""
end
row << kvhash
rescue
row << {}
end
end
headers = a[0][0..csv_columns-2]
all_tags.each do |k,v|
headers << k
all_tags[k] = headers.length-1
end
CSV.open(ARGV[1], "wb") do |csv|
csv << headers
a[1..a.length-2].each do |row|
new_row = row[0..-3]
row[-1].each do |k,v|
new_row[all_tags[k]] = v
end
if new_row.length < headers.length
new_row[headers.length-1] = nil
end
csv << new_row
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment