[EMAIL PROTECTED] wrote on 19/12/2007 11:34:30 AM:
> Norman Gaywood wrote:
>
> >> Oddly, perl very rarely wins these. ;-)
> > This must come close:
> >
> > perl -00 -ne 'print tr/,//' input.txt
>
> Timing test: say the above takes time 1.0
> then the following takes time 0.46 ...
>
> >> python -Sc "import sys; print sys.stdin.read().count(',')"
>
> both are much much faster than a sequence of pipes and
> low-level shell utils. And easier to comprehend.
>
> IMHO, the most important part of optimisation these days
> is comprehensibility: people power costs a lot more than
> CPU and disks power.
>
> I nominate the perl soln as the winner so far: runs like
> a bat of out hell and is the most easy to understand.
> And the shortest in source code size.
I have to disagree. Whilst it may be fast, its not 100% correct.
Most of the time it would probably work, but if there are any blank lines,
it outputs the current count, and starts again.
Consider the following file contents:
--file contents--
this,is,the,first,line
this,is,the,second
the,above,was,a,blank,line
and,another,blank,line
--end file contents--
On Jeff's original command:
sed 's#[^,]*##g' input.txt | tr -d '\n' | wc -m
15
The perl command:
perl -00 -ne 'print tr/,//' input.txt
753
I'm not skilled enough with perl to correct this, but
If the numbers could easily be added together it would work...
Cheers,
Scott
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html