[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: string occurrences

Quoting Clive Standbridge on 2010-01-26 15:58:00:
> Brian seems to have thought of everything.

Not everything :) Trying to learn, to the point of mastery, two
languages at once though.

Eric's (no pipe to wc) works far better when you're just interested in
the number of lines that match, and not interested in the count -- and
runs faster to boot. As an example, given the file [1]...

foobar foobaz foovax kremvax
vaxvax super-foonly foo fighters
[end 1]

If we wonder only how many lines have 'foo' in them, and we use Eric's
construct, we get the following:

bryans@esterhazy:~$ do strings /tmp/foo | grep -oce foo # prints 3

Slightly different tools for slightly different problems, I guess :)

Now, running each 1000 times on my 1.1GHz P3 laptop: 

bryans@esterhazy:~$ time for x in $(seq 1000); do strings /tmp/foo |
grep -oce foo >/dev/null; done # How many lines contain 'foo'?

bryans@esterhazy:~$ time for x in $(seq 1000); do strings /tmp/foo |
grep -oe foo | wc -l >/dev/null; done # How many times does 'foo' occur?

	Eric	Brian
real    8.675	12.172
user    2.948	 4.008
sys     5.340	 7.800

This thread actually gave me a slightly better understanding of grep and
pipelines. I assumed that adding a pipe would slow it down a bit, but
never could get off my corpulent posterior to look into put some hard
data into my hyptothesis.

 _  Brian Ryans 8B2A 54C4 E275 8CFD 8A7D 5D0B 0AD0 B014 C112 13D0     .
( ) ICQ UIN: 43190205 | Mail/MSN/Jabber: BrianLRyans@gmail.com       ..:
 X  ASCII ribbon campaign against HTML mail and v-cards: asciiribbon.org
/ \ This is my .signature. There are many like it, but this one is mine.

Attachment: signature.asc
Description: Digital signature

Reply to: