How to count lines in a document? How to count lines in a document? bash bash

How to count lines in a document?


Use wc:

wc -l <filename>

This will output the number of lines in <filename>:

$ wc -l /dir/file.txt3272485 /dir/file.txt

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

$ wc -l < /dir/file.txt3272485

You can also pipe data to wc as well:

$ cat /dir/file.txt | wc -l3272485$ curl yahoo.com --silent | wc -l63


To count all lines use:

$ wc -l file

To filter and count only lines with pattern use:

$ grep -w "pattern" -c file  

Or use -v to invert match:

$ grep -w "pattern" -c -v file 

See the grep man page to take a look at the -e,-i and -x args...