Tuesday, May 10, 2011

reading from file or stream

check  Occurrences of words in file

What if you had to use if frequently.. simple put it in shell program,
and allow it to either read the given file or from the stream, like wc does.

man bash|col -b > bash.txt

$ wc  bash.txt
  4910  37273 260193 bash.txt

$ cat bash.txt |wc
   4910   37273  260193

the below program does it 'occurrences'

                                                                                                                                                     
#!/bin/bash                                                                                                                                     
#program: occurrences
file=${1:--}                                                                                                                                  
cat $file | sed -e 's/[^a-zA-Z0-9.]/ /g' | xargs | tr ' ' '\n' | sort |  uniq -c | sort -nr -k1,2          
                                                                                                                                                      




$ cat bash.txt | occurrences

   2969 the
   1501 is
    948 to
    944 of
    868 a
    620 and
..
..

No comments:

Post a Comment