How can I count the number of lines of a file with common tools?

If you have already collected the grep output in a file, you could output a numbered list with:

cat -n myfile

If you only want the number of lines, simply do:

wc -l myfile

There is absolutely no reason to do:

cat myfile | wc -l

...as this needlessly does I/O (the cat) that wc has to repeat. Besides, you have two processes where one suffices.

If you want to grep to your terminal and print a count of the matches at the end, you can do:

grep whatever myfile | tee /dev/tty | wc -l

The -c flag will do the job. For example:

 grep -c ^ filename

will count the lines returned by grep.

Documented in the man page:

-c, --count Suppress normal output; instead print a count of matching lines for each input file


Use

your_command | wc -l

From the manual:

NAME
       wc - print newline, word, and byte counts for each file

...

       -l, --lines
          print the newline counts

Tags:

Grep

Files