Debian Codegraph @

wf2.sh

raw symbols SCIP
1 #!/bin/bash
2 # wf2.sh: Crude word frequency analysis on a text file.
3
4 # Uses 'xargs' to decompose lines of text into single words.
5 # Compare this example to the "wf.sh" script later on.
6
7
8 # Check for input file on command-line.
9 ARGS=1
10 E_BADARGS=85
11 E_NOFILE=86
12
13 if [ $# -ne "$ARGS" ]
14 # Correct number of arguments passed to script?
15 then
16 echo "Usage: `basename $0` filename"
17 exit $E_BADARGS
18 fi
19
20 if [ ! -f "$1" ] # Does file exist?
21 then
22 echo "File \"$1\" does not exist."
23 exit $E_NOFILE
24 fi
25
26
27
28 #####################################################
29 cat "$1" | xargs -n1 | \
30 # List the file, one word per line.
31 tr A-Z a-z | \
32 # Shift characters to lowercase.
33 sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\
34 /g' | \
35 # Filter out periods and commas, and
36 #+ change space between words to linefeed,
37 sort | uniq -c | sort -nr
38 # Finally remove duplicates, prefix occurrence count
39 #+ and sort numerically.
40 #####################################################
41
42 # This does the same job as the "wf.sh" example,
43 #+ but a bit more ponderously, and it runs more slowly (why?).
44
45 exit $?