...making Linux just a little more fun!
Ben Okopnik [ben at linuxgazette.net]
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 * https://LinuxGazette.NET *
Jim Jackson [jj at franjam.org.uk]
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 * https://LinuxGazette.NET * > > TAG mailing list > TAG at lists.linuxgazette.net > https://lists.linuxgazette.net/listinfo.cgi/tag-linuxgazette.net >
Ben Okopnik [ben at linuxgazette.net]
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 * https://LinuxGazette.NET *