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!

Linux performs well in servers and development environments, while Windows performs better in desktop and gaming. 1) Linux's file system performs well when dealing with large numbers of small files. 2) Linux performs excellently in high concurrency and high throughput network scenarios. 3) Linux memory management has more advantages in server environments. 4) Linux is efficient when executing command line and script tasks, while Windows performs better on graphical interfaces and multimedia applications.

Creating graphical user interface (GUI) applications is a fantastic way to bring your ideas to life and make your programs more user-friendly. PyGObject is a Python library that allows developers to create GUI applications on Linux desktops using the

Arch Linux provides a flexible cutting-edge system environment and is a powerfully suited solution for developing web applications on small non-critical systems because is a completely open source and provides the latest up-to-date releases on kernel

Due to its Rolling Release model which embraces cutting-edge software Arch Linux was not designed and developed to run as a server to provide reliable network services because it requires extra time for maintenance, constant upgrades, and sensible fi
![12 Must-Have Linux Console [Terminal] File Managers](https://img.php.cn/upload/article/001/242/473/174710245395762.png?x-oss-process=image/resize,p_40)
Linux console file managers can be very helpful in day-to-day tasks, when managing files on a local machine, or when connected to a remote one. The visual console representation of the directory helps us quickly perform file/folder operations and sav

qBittorrent is a popular open-source BitTorrent client that allows users to download and share files over the internet. The latest version, qBittorrent 5.0, was released recently and comes packed with new features and improvements. This article will

The previous Arch Linux LEMP article just covered basic stuff, from installing network services (Nginx, PHP, MySQL, and PhpMyAdmin) and configuring minimal security required for MySQL server and PhpMyadmin. This topic is strictly related to the forme

Zenity is a tool that allows you to create graphical dialog boxes in Linux using the command line. It uses GTK , a toolkit for creating graphical user interfaces (GUIs), making it easy to add visual elements to your scripts. Zenity can be extremely u


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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