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

cat file | grep * -c
link|improve this question
feedback

3 Answers

You can use this command:

wc -l <file>

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

link|improve this answer
feedback

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.

link|improve this answer
feedback

Number all non-empty lines:

cat file | nl

Or include everything:

cat file | nl -ba
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.