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

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


Reply to: