tr command in the statistics of the frequency of English words

  • 2021-01-06 00:49:39
  • OfStack

The tr command we know very well, can delete substitution, delete strings. In English, we will often often count the frequency of the occurrence in English. If we use the conventional method, use the setting calculator to calculate each one is more difficult. At this time, use the tr command, replace the space segmentation with the newline character, and then use the tr command to delete some words after the dot, comma, exclamation mark. Take a look at the this.txt file you want to replace


The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!


For the above text file, to count the top 10 words in the text, you can use the following command

[root@linux ~]# cat this.txt | tr ' ' '\n' | tr -d '[.,!]' | sort | uniq -c | sort -nr | head -10
10 is
8 better
8 than
5 to
5 the
3 of
3 Although
3 never
3 be
3 one

It is very convenient!


Related articles: