Skip to main content
added 401 characters in body
Source Link
JoL
  • 5k
  • 1
  • 20
  • 37

Simple, though doesn't care if it reads the file many times:

sed 's/ /\n/g' file.txt | sort | uniq | while read -r word; do printf "%s:%d\n" "$word" "$(grep -Fw $word"$word" file.txt | wc -l)" done 

EDIT: Despite converting spaces to newlines, this does count lines that have an occurrence of each word and not the occurrences of the words themselves. It gives the result:

0:1 1:1 2:1 a:1 different:1 hello:1 is:3 man:2 one:1 possible:1 the:3 this:1 world:2 

which is character-by-character identical to OP's example result.

Simple, though doesn't care if it reads the file many times:

sed 's/ /\n/g' file.txt | sort | uniq | while read word; do printf "%s:%d\n" "$word" "$(grep -Fw $word file.txt | wc -l)" done 

Simple, though doesn't care if it reads the file many times:

sed 's/ /\n/g' file.txt | sort | uniq | while read -r word; do printf "%s:%d\n" "$word" "$(grep -Fw "$word" file.txt | wc -l)" done 

EDIT: Despite converting spaces to newlines, this does count lines that have an occurrence of each word and not the occurrences of the words themselves. It gives the result:

0:1 1:1 2:1 a:1 different:1 hello:1 is:3 man:2 one:1 possible:1 the:3 this:1 world:2 

which is character-by-character identical to OP's example result.

Post Undeleted by JoL
Post Deleted by JoL
Source Link
JoL
  • 5k
  • 1
  • 20
  • 37

Simple, though doesn't care if it reads the file many times:

sed 's/ /\n/g' file.txt | sort | uniq | while read word; do printf "%s:%d\n" "$word" "$(grep -Fw $word file.txt | wc -l)" done