Home  >  Article  >  How to check the number of lines in a file in Linux

How to check the number of lines in a file in Linux

DDD
DDDOriginal
2023-06-08 16:16:075566browse

Linux method to check the number of lines in a file: 1. wc command. The wc command plus parameter "-l" can check the number of lines in the file; 2. cat and wc commands. Use the cat command to output the file content to Terminal, and then use the wc command to count the number of lines; 3. sed command, use "sed -n" to view the number of lines in the file; 4. awk command, use "awk 'END{print NR}'" to view the number of lines in the file.

How to check the number of lines in a file in Linux

#The operating environment of this article: linux5.18.14 system, dell g3 computer.

1. Use the wc command

The wc command can count the number of lines, the number of words, the number of bytes and other information of the file. Use the wc command to view the number of lines in the file, just add the parameter "-l". For example:

wc -l file.txt

The above command will display the line number of the file.txt file.

2. Use the cat and wc commands

Use the cat command to output the file content to the terminal, and then use the wc command to count the number of lines, for example:

cat file.txt | wc -l

3. Use sed Command

The sed command is a powerful text processing tool that can be used to find, replace, and delete specified content. To use the sed command to view the number of lines in a file, you can use the following command:

sed -n '$=' file.txt

4. Use the awk command

The awk command is a text processing tool that can be used to find, replace, and extract Text messages, etc. Use the awk command to view the number of lines in a file. You can use the following command:

awk 'END{print NR}' file.txt

The above is the detailed content of How to check the number of lines in a file in Linux. 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