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]... [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: bry...@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: bry...@esterhazy:~$ time for x in $(seq 1000); do strings /tmp/foo | grep -oce foo >/dev/null; done # How many lines contain 'foo'? bry...@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? EricBrian real8.675 12.172 user2.9484.008 sys 5.3407.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: brianlry...@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. signature.asc Description: Digital signature
Re: Re: string occurrences
On Tue, Jan 26, 2010 at 10:58 PM, Clive Standbridge wrote: > Eric Gerlach wrote: >> On Sat, Jan 23, 2010 at 04:11:03PM -0600, Brian Ryans wrote: >> > Quoting roberto on 2010-01-23 15:33:53: >> > > is there any linux built-in utilities to count how many times a >> > string >> > > occur in a text file ? >> > >> > I don't know of any actual utilities to do so, but there's a handy >> > little pipeline that I use as a generic string-counter that, so far, >> > works for all files I've tried, printable or not. >> > >> > $ strings $yourFile | grep -oe '$yourString' | wc -l thank you, i'll test it soon -- roberto -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Re: Re: string occurrences
Eric Gerlach wrote: > On Sat, Jan 23, 2010 at 04:11:03PM -0600, Brian Ryans wrote: > > Quoting roberto on 2010-01-23 15:33:53: > > > is there any linux built-in utilities to count how many times a > > string > > > occur in a text file ? > > > > I don't know of any actual utilities to do so, but there's a handy > > little pipeline that I use as a generic string-counter that, so far, > > works for all files I've tried, printable or not. > > > > $ strings $yourFile | grep -oe '$yourString' | wc -l > > It's a bit nitpicky, but you could save a step and a few keystrokes > with: > > $ strings $yourFile | grep -oce '$yourString' I thought so too, but trying it out proved otherwise, and taught me a couple of points: "grep -o" outputs each matching string on a separate line, so if there are two matches on a line, there will be two lines in the output. "grep -c" counts the number of lines which match. "grep -oc" behaves the same as "grep -c". In effect the -o is cancelled out by the -c. Brian seems to have thought of everything. -- Cheers, Clive -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Re: string occurrences
On Sat, Jan 23, 2010 at 04:11:03PM -0600, Brian Ryans wrote: > Quoting roberto on 2010-01-23 15:33:53: > > is there any linux built-in utilities to count how many times a string > > occur in a text file ? > > I don't know of any actual utilities to do so, but there's a handy > little pipeline that I use as a generic string-counter that, so far, > works for all files I've tried, printable or not. > > $ strings $yourFile | grep -oe '$yourString' | wc -l It's a bit nitpicky, but you could save a step and a few keystrokes with: $ strings $yourFile | grep -oce '$yourString' Cheers, -- Eric Gerlach, Network Administrator Federation of Students University of Waterloo p: (519) 888-4567 x36329 e: egerl...@feds.uwaterloo.ca -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Re: string occurrences
roberto wrote: hello is there any linux built-in utilities to count how many times a string occur in a text file ? ps: sorry for the italian post Yes. man wc Wayne -- To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Re: string occurrences
roberto wrote at 2010-01-23 15:33 -0600: > is there any linux built-in utilities to count how many times a string > occur in a text file ? grep "string" file | wc -l will give you the number of *lines* that contain a specific string. Only counts 1 if the string exists twice in a line, or 0 if it spans multiple lines. signature.asc Description: Digital signature
Re: string occurrences
Quoting roberto on 2010-01-23 15:33:53: > is there any linux built-in utilities to count how many times a string > occur in a text file ? I don't know of any actual utilities to do so, but there's a handy little pipeline that I use as a generic string-counter that, so far, works for all files I've tried, printable or not. $ strings $yourFile | grep -oe '$yourString' | wc -l where $your{File,String} are the file you want to search, and the string (actually a regex) you want to search for, respectively. These are extremely powerful commands, and from my experience are all fairly universal. For more information, see their relevant manpages: strings(1), grep(1), and wc(1). -- _ ASCII Ribbon Campaign Against ( ) Brian Ryans HTML E-mail and V-cards Xbrianlry...@gmail.com www.asciiribbon.org / \ GPG Public Key 0xC11213D0 Key Fingerprint: 8B2A 54C4 E275 8CFD 8A7D 5D0B 0AD0 B014 C112 13D0 signature.asc Description: Digital signature