For example:
cat /etc/passwd
What is the easiest way to count and display the number of lines the command outputs?
Join Stack Overflow to learn, share knowledge, and build your career.
For example:
cat /etc/passwd
What is the easiest way to count and display the number of lines the command outputs?
wc
is the unix utility which counts characters, words, lines etc. Try man wc
to learn more about it. The -l
option makes it print only the number of lines (and not characters and other stuff).
So, wc -l <filename>
will print the number of lines in the file <filename>
.
You asked about how to count number of lines output from a command line program in general. To do that, you can use pipes in unix. So, you can pipe the output of any command to wc -l
. In your example, cat /etc/password
is the command line program you want to count. For that you should do:
cat /etc/password | wc -l
wc -l < /etc/password
– Prince John Wesley Jun 6 '12 at 8:49wc
accept a filename, so:wc -l /etc/passwd
– kev Jun 6 '12 at 9:27cat
is being used as a stand-in for another command (otherwise it's a UUoC. – Dennis Williamson Jun 6 '12 at 10:54