FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 01-23-2010, 08:33 PM
roberto
 
Default string occurrences

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
--
roberto


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 01-23-2010, 09:11 PM
Brian Ryans
 
Default 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 X BrianLRyans@gmail.com
www.asciiribbon.org / GPG Public Key 0xC11213D0
Key Fingerprint: 8B2A 54C4 E275 8CFD 8A7D 5D0B 0AD0 B014 C112 13D0
 
Old 01-23-2010, 09:22 PM
green
 
Default 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.
 
Old 01-23-2010, 09:43 PM
"Wayne
 
Default 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-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 01-26-2010, 02:13 PM
Eric Gerlach
 
Default 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: egerlach@feds.uwaterloo.ca


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 01-26-2010, 08:58 PM
Clive Standbridge
 
Default 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-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 
Old 01-27-2010, 07:47 AM
Brian Ryans
 
Default 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:

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.
 
Old 01-28-2010, 11:02 AM
roberto
 
Default string occurrences

On Tue, Jan 26, 2010 at 10:58 PM, Clive Standbridge
<Clive.Standbridge@myriadgroup.com> 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-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
 

Thread Tools




All times are GMT. The time now is 09:31 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org