counting word in a file using shell script

How would you count word in file. remember you can't use grep with wc because it won't count word coming twice in same line.
Why are you asking this in a C++ forum?
I can do it in perl.. :D
I can do it in Python... :D
I can do it in Brainfuck!
No, I can't D:
closed account (S6k9GNh0)
LOLCODE ftw.

And I've never used wc. What does it do?
Last edited on
Counts words, chars or lines in a file or files. At first I thought it only accepted one file; so I wrote my own version.

Two hours later I man'd it and discovered I was wrong...
Last edited on
closed account (S6k9GNh0)
so wouldn't "cat <filename> | wc" work?
Last edited on
No idea... I've not delved very deep into the shell yet. I'm still trying to scratch the surface out of the way so I can get to the softer core.
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#! /bin/sh
# Read a text stream on standard input, and output a list of 
# the n (default: 25) most frequently occurring words and
# their frequency count, in order of descending counts, on
# standard output.
#
# Usage:
#             wf [n]

tr -cs A-Za-z\' '\n' |
  tr A-Z a-z |
    sort |
      uniq -c |
        sort -k1,1nr -k2 |
          sed ${1:-25}q 

./wf 10 < testdoc
4 a
   4 i
   4 the
   3 queue
   3 to
   2 am
   2 global
   2 is
   2 within
   1 and


amazing 'Grey wolf'. but its too complex. I tried it and it works. can't there be better solution. an easy one. ;)
closed account (z05DSL3A)
Ooops, misread your requirements, for some reason I thought you wanted to count how many of each individual word there are.

Edit:
Actually I'm not sure what your exact requirements are, can you elucidate?
Last edited on
you have done better than needed.
e.g.
"white dog may or may not run fast.
It may rain today."
here "may" comes twice in same line and once in other line. I just want to find out how many counts of "may" are.
Topic archived. No new replies allowed.