Master the Linux command word count (wc)!
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:
- -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
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
类似这样的用法还有很多,只要你想统计都可以做。
这里再多说两句:
- |是管道符,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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

Sublime Text provides shortcuts to improve development efficiency, including commonly used (save, copy, cut, etc.), editing (indentation, formatting, etc.), navigation (project panel, file browsing, etc.), and finding and replacing shortcuts. Proficiency in using these shortcut keys can significantly improve Sublime's efficiency.
