In Linux, you can use the wc command to count the number of lines in a file. The function of this command is to count the number of bytes, words, and lines in the specified file, and display and output the statistical results. When the parameter is set to When "-l" is used, the number of lines in the file will be counted, and the syntax is "wc -l file name".
#The operating environment of this article: linux7.3 system, Dell G3 computer.
Using the wc command, we can calculate the number of Bytes, words, or columns of the file, and count the number of bytes, words, and lines in the specified file. , and display and output the statistical results.
The syntax is:
wc [选项] 文件 ...
Common parameters are as follows:
-c counts the number of bytes.
-l counts the number of rows.
-m Counts the number of characters. This flag cannot be used with the -c flag.
-w Count the number of words. Note that the words here refer to strings separated by spaces, newlines, etc.
The example is as follows:
Statistics on the number of file lines, words and bytes
$ wc test.txt 1 1 7 test.txt
Print results Indicates 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 words
There are only two word.
Only count the number of file lines, words, characters or bytes
When counting only 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
Programming, here are two characters, and the occupied space is 6 bytes, but using wc -m statistics will be more than two One more, which is 3 characters.
$ echo 编程|wc -m 3 $ echo 编程|wc -c 7
[Related recommendations: laravel video tutorial]
The above is the detailed content of How to count the number of lines in a file in Linux. For more information, please follow other related articles on the PHP Chinese website!