| 1 | #!/bin/bashscip-shell filesystem . . `/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 [scip-shell system . . `[`. $# -ne "$ARGS" ] |
| 14 | # Correct number of arguments passed to script? |
| 15 | then |
| 16 | echoscip-shell system . . echo. "Usage: `basenamescip-shell system . . basename. $0` filename" |
| 17 | exit $E_BADARGS |
| 18 | fi |
| 19 | |
| 20 | if [scip-shell system . . `[`. ! -f "$1" ] # Does file exist? |
| 21 | then |
| 22 | echoscip-shell system . . echo. "File \"$1\" does not exist." |
| 23 | exit $E_NOFILE |
| 24 | fi |
| 25 | |
| 26 | |
| 27 | |
| 28 | ##################################################### |
| 29 | catscip-shell system . . cat. "$1" | xargsscip-shell system . . xargs. -n1 | \ |
| 30 | # List the file, one word per line. |
| 31 | trscip-shell system . . tr. A-Z a-z | \ |
| 32 | # Shift characters to lowercase. |
| 33 | sedscip-shell system . . 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 | sortscip-shell system . . sort. | uniqscip-shell system . . uniq. -c | sortscip-shell system . . 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 $? |