1

For example:

cat /etc/passwd

What is the easiest way to count and display the number of lines the command outputs?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Ayrx
  • 2,092
  • 5
  • 26
  • 34
  • `wc -l < /etc/password ` – Prince John Wesley Jun 06 '12 at 08:49
  • 1
    @Raze2dust How about putting your comment in an answer so it can be ticked correct? Possibly without the Google commment. Thanks. – Thomas Jun 06 '12 at 08:49
  • 1
    `wc` accept a filename, so: `wc -l /etc/passwd` – kev Jun 06 '12 at 09:27
  • @Thomas If I had created questions for everything I wanted to know about and gotten immediate answers, I wouldn't have learned 10% of what I know today. I should probably not have put the answer at all, because it defeats the purpose of the google comment. – Hari Menon Jun 06 '12 at 10:20
  • @kev: If you use redirection, the output consists only of the count without the filename being repeated back to you. I imagine in this case `cat` is being used as a stand-in for another command (otherwise it's a UUoC. – Dennis Williamson Jun 06 '12 at 10:54
  • Duplicates: http://stackoverflow.com/questions/3137094/how-to-count-lines-on-a-document, http://stackoverflow.com/questions/114814/count-non-blank-lines-of-code-in-bash, http://stackoverflow.com/questions/6314679/in-bash-how-do-i-count-the-number-of-lines-in-a-variable, http://stackoverflow.com/questions/1412244/use-find-wc-and-sed-to-count-lines, *ad nauseum*. Moderators, please merge answer with some existing question; the first one looks closest to me. – Todd A. Jacobs Jun 06 '12 at 11:13
  • @Raze2dust See, next time some one googles this problem, they might well find this page on SO and be happy for your answer. – Thomas Jun 07 '12 at 01:01

1 Answers1

7

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
Hari Menon
  • 33,649
  • 14
  • 85
  • 108