Skip to content

Instantly share code, notes, and snippets.

@ryanfb
Created July 11, 2022 21:49
Show Gist options
  • Save ryanfb/7c587b77c47db4fa163cc2aa3b0bb96a to your computer and use it in GitHub Desktop.
Save ryanfb/7c587b77c47db4fa163cc2aa3b0bb96a to your computer and use it in GitHub Desktop.
Get the total number of bytes used for each sender in an Mbox email file
#!/usr/bin/env ruby
emails = {}
email_length = 0
last_email_from = nil
File.open(ARGV[0], "r:ASCII-8BIT").each_line do |line|
if line.start_with?('From ')
unless last_email_from.nil?
emails[last_email_from] ||= 0
emails[last_email_from] += email_length
last_email_from = nil
end
email_length = 0
end
if line.start_with?('From: ')
parsed_address = line.sub(/^From: /, '')
if parsed_address =~ /<.+>/
parsed_address = parsed_address.split('<')[1].split('>').first
end
last_email_from = parsed_address
end
email_length += line.length
end
emails.sort_by{|from, length| length}.reverse.each do |result|
puts "#{result.last} #{result.first}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment