I'm in the middle of a periodic email purge at the moment. I want to count how many messages I have scattered across my mailboxes. On the mac, where I'm using Mail.app:
cd ~/Library/Mail/Mailboxes find . -name mbox | sed -e 's/^\(.*\)$/"\1"/' | xargs grep -c '^From ' | cut -f 2 -d : | totalOn the mailbox at my ISP, where I use mutt:
cd ~/Mail find . -type f | xargs grep -c '^From ' | cut -f 2 -d : | totalWhere total is:
#!/usr/local/bin/perl $total += $_ while (<>); print "$total\n";
Whenever I see a big text processing pipeline in my stuff I think, "I'll combine it into a single Perl script". The funny part is that the one Perl script you have in your note is the one case where I still fall back on using awk
, as in:
That silly script is burned into my neurons from pre-Perl days, and refuses to go away....awk 's += $1; END {print s}'
Re:Not Perl?
ziggy on 2005-01-07T16:36:54
I dunno. I've got for loops and command pipelines wired into my hindbrain. I'd rather type out a long command than write a perl script for something like this.If I execute a long command often enough, I do write a perl script for it; writing a perl script for a command I execute once or infrequently feels like a premature optimization.
Perl+= AWK Re:Not Perl?
n1vux on 2005-01-07T19:12:04
The AWK oneliner still works in Perl.-a says load @F array with AWK fields. And even better, $F[-1] is the last item.perl -lane '$s += $F[1]; END{print $s}'
Optimizing for hack value:
( sed 'a \ +' ; echo 0 ) | xargs expr
Optimizing for minimum overhead:
{ total=0 ; while read c ; do (( total += c )) ; done ; echo $total ; }