Basically my problem is this, I have a string delimited by periods, I want to be able to print each word on a new line with the number of times it occurs next to it.
Here is what I have already:
EDIT:
#!/bin/bash
PARAM=$1
FILE=${1-test.txt}
#echo $FILE
temp=$( tr '\n' '.' <$FILE )
arr=$(echo $temp | tr "." "\n")
for x in $arr
do
echo "$x"
done
All this does is print out each word on a line. Now I need to count the number of times each word appears and then print it out next to the word. For example:
temp contents = apple, pear, apple, peach
Output should be:
apple 2
pear 1
apple 2
peach 1
where each word is on a new line.
uniq(1)can probably do what you want. For example:Note that
uniqrequires sorted input to be able to properly count occurrences.