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

Master the Linux command word count (wc)!

Feb 14, 2024 pm 09:50 PM
linux linux tutorial linux system linux command shell script embeddedlinux Getting started with linux linux learning

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

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.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

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.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

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.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

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.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

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.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

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)

git software installation git software installation Apr 17, 2025 am 11:57 AM

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)

How to use sublime shortcut keys How to use sublime shortcut keys Apr 16, 2025 am 08:57 AM

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.

See all articles