Skip to content

Instantly share code, notes, and snippets.

@ninp0
Last active October 2, 2024 16:07
Show Gist options
  • Select an option

  • Save ninp0/b6df41f0d0b903b2d6e5419b28c02877 to your computer and use it in GitHub Desktop.

Select an option

Save ninp0/b6df41f0d0b903b2d6e5419b28c02877 to your computer and use it in GitHub Desktop.
#!/user/bin/env ruby
# frozen_string_literal: true
require 'csv'
require 'optparse'
opts = {}
OptionParser.new do |options|
options.banner = "USAGE:
#{File.basename($PROGRAM_NAME)} [opts]
"
options.on('-iCSV', '--input-csv=CSV', '<Required - Path of Procmon CSV Results to Extract Unique Path Entries>') do |i|
opts[:input_csv_file_path] = i
end
end.parse!
if opts.empty?
puts `#{File.basename($PROGRAM_NAME)} --help`
exit 1
end
def uni(opts = {})
arr = opts[:arr]
key = opts[:key]
seen = {}
arr.each_with_object([]) do |hash, result|
unless seen[hash[key]]
seen[hash[key]] = true
result << hash
end
end
end
timestamp = Time.now.strftime('%Y-%m')
input_csv_file_path = opts[:input_csv_file_path]
raise "ERROR: #{input_csv_file_path} Not Found." unless File.exist?(input_csv_file_path)
output_csv_file_path = "#{timestamp}-UNIQUE-#{input_csv_file_path}"
data = CSV.parse(
File.read(input_csv_file_path),
headers: true,
liberal_parsing: true
)
data_array = data.map(&:to_hash)
uniq_data_array = data_array.uniq
unique_key = 'Path'
uniq_data_array = uni(
arr: uniq_data_array,
key: unique_key
)
CSV.open(output_csv_file_path, 'w', write_headers: true, headers: data.headers) do |csv|
uniq_data_array.each do |row|
csv << row.values
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment