Showing the line count of a specific file

5

1

Is this the correct way to show the line count of a specific file?

cat file | grep * -c

Test3r

Posted 2011-03-30T06:48:51.357

Reputation: 67

Answers

16

You can use this command:

wc -l <file>

This will return the total line number count in the provided file.

Gaff

Posted 2011-03-30T06:48:51.357

Reputation: 16 863

1

Your shell will "expand" the asterisk in grep * -c to everything in the current directory, resulting in, for instance:

grep foo bar baz -c

Which is not what you want.

Try cat file | grep -c . to count the number of rows containing at least a printable character, or cat file | wc -l to count the number of lines.

If the input is a file, however, you may consider giving access to the file instead of piping it on stdin, to the command that does the counting. (for example wc -l file or grep . -c file).

If you don't want wc to show the filename when giving it a filename, you can extract the first word of the output of wc -l with your favorite filter, such as cut(1): wc -l foo | cut -d' ' -f 1 or awk(1): wc -l foo | awk '{print $1}', or something else with the same effect.

MattBianco

Posted 2011-03-30T06:48:51.357

Reputation: 1 763

0

Number all non-empty lines:

cat file | nl

Or include everything:

cat file | nl -ba

Bill Snoofus

Posted 2011-03-30T06:48:51.357

Reputation: 9