Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the wc command, an efficient data statistics tool for Linux systems

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use the Linux system efficient data statistics tool wc command, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Wc (world count) is a Linux command that counts the number of words, bytes and lines in a file. It can help us to count the above information conveniently.

Main parameters

Common parameters are as follows:

C counts the number of bytes.

-l count the number of rows.

-m counts the number of characters. This flag cannot be used with the-c flag.

-w count the word count. Note that the words here refer to strings separated by spaces, newline characters, and so on.

Let's go straight to a few examples.

Count the number of lines, words and bytes of files

$wc test.txt 1 1 7 test.txt

The print results show that the file has 1 line, 1 word, and 7 bytes.

In particular, the words here are strings separated by spaces, newline characters, etc., that is to say,

Words words

There are only two words here.

Count only the number of lines, words, characters or bytes of files

When you only count individual items, you only need to take the corresponding parameters, for example:

$wc-l test.txt1 test.txt

Use the-l argument so that only the number of rows is displayed.

However, special attention should be paid to the difference between the number of characters and the number of bytes. The number of bytes is the amount of space occupied by the data, while a character may take up more than one byte. For example, in UTF-8 encoding, an English letter is a character that takes up one byte of space, while a Chinese character takes up 3 bytes.

For example:

Programming

Programming, in this case, is two characters, and the footprint is 6 bytes, but using wc-m statistics will be one more than two, which is three characters.

$echo programming | wc-m3 $echo programming | wc-c7

The occupation of each encoded character is as follows:

Encode English letter Chinese UTF-81 byte 3 bytes Unicode1 byte 2 bytes

You can use:

$echo $LANGen_GB.UTF-8

View the encoding format.

Count the number of command execution results

In fact, I think that the most commonly used parameter is the-l parameter, which is used to count the number of lines in a file or standard output, so it can actually be used to do a lot of statistical things.

For example, count how many ordinary files are in the current directory:

$ls-ltotal 4Mar RWMub RWMAY-1 hyb hyb 0 March 21 20:32 test2.txt-rw-rw-r-- 1 hyb hyb 13 March 21 20:18 test.txt$ ls-l | grep "^ -" | wc-L2

The number of files you can get is 2. Grep "^ -" means to get which lines start with -, because normal files start with -.

Of course, if you want to count the total number of files including subdirectories, you can add the-R parameter:

Ls-lR | grep "^ -" | wc-l

For example, check the number of chrome-related processes:

$ps-ef | grep google | grep-v grep | wc-L23

There are many similar uses, as long as you want to do statistics.

Here are a few more words:

| | ls-l | wc-l indicates that the result of ls-l is passed to the wc command for processing |

Grep is used for text lookups, grep "a", to find lines containing a, and grep-v "b" to filter rows containing b.

The wc command can be used to count lines, bytes, characters, etc., but it is very effective when counting the number of results of the command execution.

The above content is how to use the Linux system efficient data statistics tool wc command, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report