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.
The main parameters
Common parameters are as follows:
Let’s look at a few examples directly.
Statistics on file lines, words and bytes
$ wc test.txt 1 1 7 test.txt
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 字词
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
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:
编程
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
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
查看编码格式。
统计命令执行结果数量
实际上个人认为,最常用的还是-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
可以得到文件数量为2。grep “^-“的意思是,获取哪些以-开头的行,因为普通文件都是以-开头的。
当然如果想统计包括子目录的总文件数量,可以加上-R参数:
ls -lR |grep "^-"|wc -l
再例如,查看chrome相关进程数量:
$ ps -ef|grep google|grep -v grep |wc -l 23
类似这样的用法还有很多,只要你想统计都可以做。
这里再多说两句:
总结
本文我们学习了如何使用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!