Home > Common Problem > body text

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

DDD
Release: 2023-06-08 16:40:49
Original
5373 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!