List of all words in a text file with the number of occurrences?

Suppose I have a file text.txtas shown below:

she likes cats, and he likes cats too.

I would like my result to look like this:

she 1
likes 2
cats 2
and 1
he 1
too 1

If put space , .into it, it will simplify the scripts, it will be fine.

Is there a simple shell pipeline that could achieve this?

+5
source share
2 answers

Here is one liner close to my heart:

cat text.txt | sed 's|[,.]||g' | tr ' ' '\n' | sort | uniq -c

Suppression characters sed (adjust the regular expression to taste), tr puts the results one word at a time.

+18
source

With GNU awk, you can simply specify a record separator (RS) like any sequence of non-alphabetic characters:

$ gawk -v RS='[^[:alpha:]]+' '{sum[$0]++} END{for (word in sum) print word,sum[word]}' file
she 1
likes 2
and 1
too 1
he 1
cats 2

but this will not solve your problem, how to identify the "words" in general.

0
source

All Articles