Tux

...making Linux just a little more fun!

[2-cent Tip]: Counting your mail

Ben Okopnik [ben at linuxgazette.net]


Sat, 28 Aug 2010 13:13:02 -0400

A few minutes ago, I needed to count all the emails I had archived in my ~/Mail directory. A moment of thought, and:

grep -rc '^From ' Mail/*|awk -F: '{s+=$NF}END{print s}'

NOTES: Email headers start with a 'From ' at the beginning of the line, so each line that starts that way identifies a single email. 'grep -r' is recursive - i.e., also searches subdirectories. The output from 'grep' is a bunch of lines, each of which looks like 'file.txt:17', which says that 'file.txt' has 17 matches; therefore, we use 'awk' to split each line on colons - but since the filename itself could contain a colon, we need to grab the very last field. In 'awk', 'NF' is the count of fields in each line, and '$X' is the value of field X - so '$NF' is the value of the last field. Sum them up, print them out when it's all over, and presto - count of emails.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Jim Jackson [jj at franjam.org.uk]


Sun, 29 Aug 2010 09:58:30 +0100 (BST)

On Sat, 28 Aug 2010, Ben Okopnik wrote:

> A few minutes ago, I needed to count all the emails I had archived in my
> ~/Mail directory. A moment of thought, and:
> 
> ``
> grep -rc '^From ' Mail/*|awk -F: '{s+=$NF}END{print s}'
> ''

Nice. But I've never really internalised awk, my take would be...

grep -r '^From ' Mail/* | wc -l

cheers Jim

> 
> NOTES: Email headers start with a 'From ' at the beginning of the line,
> so each line that starts that way identifies a single email. 'grep -r'
> is recursive - i.e., also searches subdirectories. The output from
> 'grep' is a bunch of lines, each of which looks like 'file.txt:17',
> which says that 'file.txt' has 17 matches; therefore, we use 'awk' to
> split each line on colons - but since the filename itself could contain
> a colon, we need to grab the very last field. In 'awk', 'NF' is the
> count of fields in each line, and '$X' is the value of field X - so
> '$NF' is the value of the last field. Sum them up, print them out when
> it's all over, and presto - count of emails.
> 
> 
> -- 
> * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
>                                              
> TAG mailing list
> TAG at lists.linuxgazette.net
> http://lists.linuxgazette.net/listinfo.cgi/tag-linuxgazette.net
>


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Sun, 29 Aug 2010 05:45:44 -0400

On Sun, Aug 29, 2010 at 09:58:30AM +0100, Jim Jackson wrote:

> 
> 
> 
> On Sat, 28 Aug 2010, Ben Okopnik wrote:
> 
> > A few minutes ago, I needed to count all the emails I had archived in my
> > ~/Mail directory. A moment of thought, and:
> > 
> > ``
> > grep -rc '^From ' Mail/*|awk -F: '{s+=$NF}END{print s}'
> > ''
> 
> Nice. But I've never really internalised awk, my take would be...
> 
> ``
> grep -r '^From ' Mail/* | wc -l
> ''

[blink] Wonder why I didn't think of that. It's certainly more obvious.

Well done, Jim!

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back