cplusplus.com
C++ : Forum : UNIX/Linux Programming : counting word in a file using shell scri
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


post counting word in a file using shell script

reachvimalm (11)
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.
Bazzy (6258)
Why are you asking this in a C++ forum?
writetonsharma (1181)
I can do it in perl.. :D
chrisname (4916)
I can do it in Python... :D
Bazzy (6258)
I can do it in Brainfuck!
No, I can't D:
computerquip (1682)
LOLCODE ftw.

And I've never used wc. What does it do?
Last edited on
chrisname (4916)
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
computerquip (1682)
so wouldn't "cat <filename> | wc" work?
Last edited on
chrisname (4916)
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.
Grey Wolf (2846)
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


reachvimalm (11)
amazing 'Grey wolf'. but its too complex. I tried it and it works. can't there be better solution. an easy one. ;)
Grey Wolf (2846)
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
reachvimalm (11)
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.