Home >Operation and Maintenance >Linux Operation and Maintenance >What is the use of linux wc command

What is the use of linux wc command

青灯夜游
青灯夜游Original
2021-05-08 17:12:2911281browse

In Linux, the wc command is used to count the number of bytes, words, and lines in the specified file, and display and output the statistical results. If the file name is not specified, or the file name is given "-", the wc command will read data from the standard input device; the syntax of this command is "wc [option] file...".

What is the use of linux wc command

#The operating environment of this tutorial: Red Hat Enterprise Linux 6.1 system, Dell G3 computer.

Linux wc command is used to count the word count.

Using the wc command, we can calculate the number of Bytes, words, or columns of the file. If the file name is not specified, or the file name is given "-", the wc command will start from Read data from the standard input device.

Syntax:

wc [选项] 文件 ...

Parameters:

  • -c or --bytes or --chars Display only the number of Bytes.

  • -l or --lines displays the number of lines.

  • -w or --words displays only the number of words.

  • --help Online help.

  • --version Display version information.

Specific usage and analysis

The following uses specific files to demonstrate the specific usage and analysis of the above option parameters of the wc command, and creates a test.txt file, the file content is as shown below:

1) After executing the command wc -c test.txt, the result is as shown below:

As shown in the figure, the file shows 24 bytes, because it includes spaces and newlines for each line.

We can check the newline characters through the command cat -A test.txt, as shown in the following figure:

2) Execute the command wc -l test.txt After that, the result is as shown below:

The picture shows that the file has 3 lines

3) After executing the command wc -m test.txt, the result As shown in the figure below:

The result is the same as the parameter result of the -c option, with 24 characters

4) Execute the command wc -w test. txt, the result is as shown below:

The picture shows that the file has 4 words.

5) After executing the command wc -L test.txt, the result is as shown in the figure below:

The figure shows that the longest line length of the file is 11 , excluding newlines.

6) After executing the command wc test.txt, the result is as shown in the figure below:

The results in the figure are displayed in order: the number of lines, the number of words, and the number of words. Number of sections, file name.

4. The above is the detailed parameter option usage for the wc command. In actual applications, it is also used with the pipe character, which is very useful when writing shell scripts. The following is an example:

1) After executing the command ls -l in the root directory, you will see the printed information as shown below:

2) In the root directory After executing the command ls -l | wc -l in the directory, the result is as shown in the figure below:

The result shows that the statistics displayed by the ls -l command include 20 lines.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is the use of linux wc command. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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