Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

cat file | grep * -c
share|improve this question
add comment

3 Answers

You can use this command:

wc -l <file>

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Number all non-empty lines:

cat file | nl

Or include everything:

cat file | nl -ba
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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