Home > System Tutorial > LINUX > Master the Linux command word count (wc)!

Master the Linux command word count (wc)!

王林
Release: 2024-02-14 21:50:12
forward
753 people have browsed it

As one of the most commonly used commands in Linux systems, word count (wc) plays an important role in text processing and statistics. Whether you are a beginner or an experienced Linux administrator, it is important to master the wc command. This article will introduce in detail how to use the wc command and its application in Linux systems.

掌握Linux命令之word count(wc)!

The main parameters

Common parameters are as follows:

  • -c counts bytes.
  • -l counts the number of lines.
  • -m counts the number of characters. This flag cannot be used with the -c flag.
  • -w Count word count. Note that the words here refer to strings separated by spaces, newlines, etc.

Let’s look at a few examples directly.

Statistics on file lines, words and bytes

$ wc test.txt 
1 1 7 test.txt
Copy after login

The print result shows that the file has 1 line, 1 word, and 7 bytes.

It is important to remind that the words here are strings separated by spaces, newlines, etc., that is to say

words 字词
Copy after login

There are only two words here.

Only count the number of file lines, words, characters or bytes

When only counting a single item of content, you only need to bring the corresponding parameters, for example:

$ wc -l test.txt
1 test.txt
Copy after login

Use the -l parameter to display only the number of lines.

But what needs special attention here is the difference between the number of characters and the number of bytes. The number of bytes is the amount of space occupied by data, and a character may occupy multiple bytes. For example, in UTF-8 encoding, an English letter is a character and occupies one byte of space, while a Chinese character occupies 3 bytes. size.

for example:

编程
Copy after login

Programming, here it is two characters, and the occupied space is 6 bytes, but using wc -m statistics will be one more than two, which is 3 characters.

$ echo 编程|wc -m
3
$ echo 编程|wc -c
7
Copy after login

The characters occupied by each coded character are as follows:

coding English alphabet Chinese
UTF-8 1byte 3 bytes
Unicode 1byte 2 bytes

你可以使用:

$ echo $LANG
en_GB.UTF-8
Copy after login

查看编码格式。

统计命令执行结果数量

实际上个人认为,最常用的还是-l参数,它用来统计文件或标准输出有多少行,那么实际上就可以用来做很多统计的事情了。

例如,统计当前目录下有多少个普通文件:

$ ls -l
total 4
-rw-rw-r-- 1 hyb hyb  0 3月  21 20:32 test2.txt
-rw-rw-r-- 1 hyb hyb 13 3月  21 20:18 test.txt
$ ls -l |grep "^-"|wc -l
2
Copy after login

可以得到文件数量为2。grep “^-“的意思是,获取哪些以-开头的行,因为普通文件都是以-开头的。

当然如果想统计包括子目录的总文件数量,可以加上-R参数:

ls -lR |grep "^-"|wc -l
Copy after login

再例如,查看chrome相关进程数量:

$ ps -ef|grep google|grep -v grep |wc -l
23
Copy after login

类似这样的用法还有很多,只要你想统计都可以做。

这里再多说两句:

  • |是管道符,ls -l|wc -l表示将ls -l的结果传给wc命令处理
  • grep用于文本查找,grep “a”,表明查找包含a的行,而grep -v “b”,表明过滤包含b的行。

总结

本文我们学习了如何使用Linux命令行工具wc,包括基本语法、参数选项和示例实践。我们了解了wc如何帮助我们快速统计字符、单词和行数,在文本处理、数据分析等方面发挥着重要的作用。希望这篇文章能够对您掌握wc命令和加深对Linux系统的理解有所帮助。

The above is the detailed content of Master the Linux command word count (wc)!. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template