Unix Shell - Count word

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2006 > Count word





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Count word
David Sagenaut

2006-02-20, 8:49 pm

If I have a file like this:

xxxxxx xxxxxx test xxxxx
test xxxxx test xxxxxxxxx
xxxxxx test xxxxxxxxxxx

how can I count the word "test"? Thanks!

Alex


Janis Papanagnou

2006-02-20, 8:49 pm

David Sagenaut wrote:
> If I have a file like this:
>
> xxxxxx xxxxxx test xxxxx
> test xxxxx test xxxxxxxxx
> xxxxxx test xxxxxxxxxxx
>
> how can I count the word "test"? Thanks!
>
> Alex
>
>


One possibility...

tr ' ' '\n' | grep -c test


Janis
Ed Morton

2006-02-20, 8:49 pm

David Sagenaut wrote:
> If I have a file like this:
>
> xxxxxx xxxxxx test xxxxx
> test xxxxx test xxxxxxxxx
> xxxxxx test xxxxxxxxxxx
>
> how can I count the word "test"? Thanks!
>
> Alex
>
>


What do mean by "word"? If you had:

abc testosterone def

would you count that? What about :

this is a test.

or does "test" need to be surrounded by white-space?

This will do what you want if you just want to count ocurrences of the
string "test"

gawk -v RS="test" 'END{print (NR?NR-1:0)}' file

Regards,

Ed.
Xicheng

2006-02-21, 2:49 am

David Sagenaut wrote:
> If I have a file like this:
>
> xxxxxx xxxxxx test xxxxx
> test xxxxx test xxxxxxxxx
> xxxxxx test xxxxxxxxxxx
>
> how can I count the word "test"? Thanks!


perl -lne '$n += () = /test/g }{ print $n' myfile

Xicheng

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com