Final answer:
The BASH script 'words.sh' reads from stdin, processes input ignoring case and non-alphanumeric characters, counts word occurrences, and outputs sorted words with counts.
Step-by-step explanation:
The BASH script words.sh that computes the number of occurrences of whitespace-separated words can be written using standard UNIX commands such as tr, sort, and uniq. The script reads lines from stdin until EOF (End Of File) is reached, processes them to remove non-alphanumeric characters, converts them to lowercase, and then counts the occurrences of each word, sorting them alphabetically in the end.
Here is the BASH script that performs the task described:
#!/bin/bash
declare -A word_count
while IFS= read -r line; do
# Convert upper case to lower case and remove non-alphanumeric characters
for word in $(echo $line | tr -cs '[:alnum:]' '\\' | tr '[:upper:]' '[:lower:]'); do
# Increment the word's count
((word_count[$word]++))
done
done
# Output words and their counts, sorted
for word in "${!word_count[a]}"; do
echo "$word ${word_count[$word]}"
done | sort
Key steps in processing the input include converting uppercase letters to lowercase and stripping out non-alphanumeric characters to ensure words are uniformly counted, ignoring case and special characters. Finally, the script sorts the unique words alphabetically before outputting with their respective counts.