The magic of tr command in counting the frequency of English words

The magic of tr command in counting the frequency of English words

We are all familiar with the tr command, which can delete replacements and delete strings. In English, we often need to count the frequency of words that appear in English. If we use the conventional method and set up a calculator to count them one by one, it is quite time-consuming. At this time, use the tr command to replace the space split with a newline character, and then use the tr command to delete the period, comma, and exclamation mark after some words. First look at the this.txt file to be replaced

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, if you want to count the top 10 words that appear the most times 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’s very convenient!

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • How to deal with the prompt "Operation not permitted" when deleting files in Linux
  • Linux shell command counts the value of a column after deduplication
  • Tips for viewing History records and adding timestamps in Linux
  • Several ways to run Python programs in the Linux background
  • Creating and executing Linux shell scripts
  • Python uses paramiko to operate Linux
  • Steps to transfer files and folders between two Linux servers
  • Solutions to Files/Folders That Cannot Be Deleted in Linux
  • Android's implementation method of executing shell scripts in the Linux terminal to directly print the log of the currently running app
  • Use of Zabbix Api in Linux shell environment

<<:  Detailed explanation of the implementation method of database multi-table connection query

>>:  Detailed explanation of Vue3's sandbox mechanism

Recommend

How to build a SOLO personal blog from scratch using Docker

Table of contents 1. Environmental Preparation 2....

Sample code for realizing book page turning effect using css3

Key Takeaways: 1. Mastering CSS3 3D animation 2. ...

How to use the Linux md5sum command

01. Command Overview md5sum - Calculate and verif...

Elementui exports data to xlsx and excel tables

Recently, I learned about the Vue project and cam...

Introduction to using the MySQL mysqladmin client

Table of contents 1. Check the status of the serv...

Introduction to new features of ECMAscript

Table of contents 1. Default values ​​for functio...

Detailed tutorial on building nextcloud private cloud storage network disk

Nextcloud is an open source and free private clou...

Teach you how to install docker on windows 10 home edition

When I wrote the Redis book and the Spring Cloud ...

My personal summary of mysql 5.7 database installation steps

1.mysql-5.7.19-winx64.zip (this is the free insta...

Detailed explanation of dynamically generated tables using javascript

*Create a page: two input boxes and a button *Cod...

HTML table_Powernode Java Academy

To draw a table in HTML, use the table tag tr me...

A brief discussion of several browser compatibility issues encountered

background Solving browser compatibility issues i...

Detailed analysis of each stage of nginx's http request processing

When writing the HTTP module of nginx, it is nece...

CentOS 7 set grub password and single user login example code

There are significant differences between centos7...