linux - [Solved-5 Solutions] How to count lines in a document using Linux commands - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to count lines in a document using Linux commands ?

Linux - Solution 1:

Use wc:

wc -l <filename>
click below button to copy the code. By - Linux tutorial - team

This will output the number of lines in :

$ wc -l /dir/file.txt
3272485 /dir/file.txt
click below button to copy the code. By - Linux tutorial - team

Or, to omit the from the result use wc -l < :

$ wc -l < /dir/file.txt
3272485
click below button to copy the code. By - Linux tutorial - team

You can also pipe data to wc as well:

$ cat /dir/file.txt | wc -l
3272485
$ curl yahoo.com --silent | wc -l
63
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

To count all lines use:

$ wc -l file
click below button to copy the code. By - Linux tutorial - team

To filter and count only lines with pattern use:

$ grep -w "pattern" -c file  
click below button to copy the code. By - Linux tutorial - team

Or use -v to invert match:

$ grep -w "pattern" -c -v file 
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

wc -l <file.txt>
click below button to copy the code. By - Linux tutorial - team

Or

command | wc -l
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 4:

There are many ways. using wc is one.

wc -l file
click below button to copy the code. By - Linux tutorial - team

Others include

awk 'END{print NR}' file
click below button to copy the code. By - Linux tutorial - team
sed -n '$=' file (GNU sed)
click below button to copy the code. By - Linux tutorial - team
grep -c ".*" file
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

The tool wc is the "word counter" in UNIX and UNIX-like operating systems, you can also use it to count lines in a file, by adding the -l option, so wc -l foo will count the number of lines in foo. You can also pipe output from a program like this: ls -l | wc -l, which will tell you how many files are in the current directory.


Related Searches to - linux - linux tutorial - How to count lines in a document using Linux commands ?