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

converting man to text

This will give the text output for 'wc' man page
 
$ man wc |col -b

this man page may not be that big, try 'bash'

$ man bash|col -b

redirect it to a file, dont ask me how.. this is quite helpful, when you open the text page in you favourite editor and search and read the options on 'bash'

its quite big and does lots of things you tried using other commands.