Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got some large mailboxes and using Thunderbird, that means that I have several mbox files. These single files contain all e-mails in a particular folder. Now, I would like to get some data on the senders in a particular folder. My ideal statistic would be to get all unique senders, and the number of times their e-mail is in that folder. Ej:

  • John A: 10x
  • Maria B: 5x


I've tried some grep options but I also get X-headers if I grep only 'From:', and I'm not sure how to exclude these other headers. Anybody any idea if this can be done from the command line?

share|improve this question
up vote 4 down vote accepted

First, we need to reliably get the From header, which can be done with a restrictive grep regular expression.

% grep --no-filename --ignore-case '^From:' test.eml
From: mgorven@example.com

Next we need to count the number of occurrences, which can be done with uniq -c (which requires a sorted list).

% grep --no-filename --ignore-case '^From:' *.eml | sort | uniq --count
      1 From: mgorven@example.com
      3 From: mgorven@example.net

We can then sort the output by occurrence, to get the most frequent at the top.

% grep --no-filename --ignore-case '^From:' *.eml | sort | uniq --count | sort --general-numeric-sort --reverse
      3 From: mgorven@example.net
      1 From: mgorven@example.com
share|improve this answer
Absolutely awesome!!! Worked like a charm, thanks! – user Jun 13 '12 at 11:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.