blob: 5637c0e525e3891bbe0aeb3e626ef54563bc384a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# Integration: a word-frequency report built from shell, sed, tr and sort.
cat >text <<'TXT'
The quick brown fox jumps over the lazy dog.
The dog barks; the fox runs. A quick, quick fox!
TXT
normalize() {
# tr's ranges and -c follow the code page's collating order, which on
# z/OS (EBCDIC) makes 'A-Z' 'a-z' a mapping into punctuation and
# leaves ';' alone. sed and the [:upper:]/[:lower:] classes do not.
sed 's/[^a-zA-Z]/ /g' | tr '[:upper:]' '[:lower:]' | tr -s ' ' '\n' |
sed '/^$/d'
}
total=0
while read -r w; do
total=$((total + 1))
done <<EOF2
$(normalize <text)
EOF2
echo "words: $total"
normalize <text | sort | uniq -c | sort -k1,1nr -k2 | head -n 5 |
while read -r count word; do
printf '%-6s %3d %s\n' "$word" "$count" "$(printf '%*s' "$count" '' | tr ' ' '#')"
done
longest=
for w in $(normalize <text); do
[ ${#w} -gt ${#longest} ] && longest=$w
done
echo "longest: $longest (${#longest})"
|