Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does linux count the number of lines in a file

Shulou Source: shulou.com Published: 2022-05-31 10:51:41 09月20日 Update

Most people do not understand the knowledge of this article "linux how to count the number of lines in a file", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "linux how to count the document how many lines" article.

In linux, you can use the wc command to count how many lines there are in the file. The function of this command is to count the number of bytes, words and lines in the specified file, and display the output of the statistical results. When the parameter is set to "- l", the number of lines in the file will be counted. The syntax is "wc-l file name".

This article operating environment: linux7.3 system, Dell G3 computer.

How does linux count the number of lines in a file?

Using the wc instruction, we can calculate the number of Byte, words, or columns of a file, count the number of bytes, words and lines in a specified file, and display the statistical results to output.

The syntax is:

Wc [options] file.

Common parameters are as follows:

C counts the number of bytes.

-l count the number of rows.

-m counts the number of characters. This flag cannot be used with the-c flag.

-w count the word count. Note that the words here refer to strings separated by spaces, newline characters, and so on.

Examples are as follows:

Count the number of lines, words and bytes of files

$wc test.txt 1 1 7 test.txt

The print results show that the file has 1 line, 1 word, and 7 bytes.

In particular, the words here are strings separated by spaces, newline characters, etc., that is to say,

Words words

There are only two words here.

Count only the number of lines, words, characters or bytes of files

When you only count individual items, you only need to take the corresponding parameters, for example:

$wc-l test.txt1 test.txt

Use the-l argument so that only the number of rows is displayed.

However, special attention should be paid to the difference between the number of characters and the number of bytes. The number of bytes is the amount of space occupied by the data, while a character may take up more than one byte. For example, in UTF-8 encoding, an English letter is a character that takes up one byte of space, while a Chinese character takes up 3 bytes.

For example:

Programming

Programming, in this case, is two characters, and the footprint is 6 bytes, but using wc-m statistics will be one more than two, which is three characters.

$echo programming | wc-m3 $echo programming | wc-c7

The above is about the content of this article on "how to count the number of lines in the linux document". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Statistics files bytes characters content parameters number of words programming two that is space results commands size strings line breaks articles flags knowledge spaces Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Shulou Tech Info Huawei Linux