RICKARD NOBEL AB

RICKARD NOBEL AB

Specialists in IT infrastructure services

Menu
  • About
  • Windows
  • Networking
  • VMware
  • Storage
Menu

Counting lines in Windows command prompt

Posted on September 10, 2011September 18, 2015 by Rickard Nobel

How to easily count output lines in cmd.exe with the builtin FIND tool.

When working in a command line environment it is sometimes very useful to be able to count the lines of output from different tools. Many Unix/Linux operating systems contains the wc tool with many options and there is no real builtin alternative in Windows, however some of same capabilities exists native in the Windows Command Prompt (cmd.exe).

In this short post we will see how we can use the FIND utility for counting lines in cmd.exe. The find tool, somewhat similar to grep in Unix, has been around since MS-DOS and is easy to use since it is included by default in every Windows installation.
 
 
 

Assume for example that we have a Windows Server and want to see how may TCP sessions that is active for the moment. This could be studied with the netstat command and piped into FIND to grep the established sessions.

netstat -nao | find /i “estab”

Established TCP sessions

The output from this could quite overwhelming with hundreds of lines and we might only be interesting in the number of sessions. By just appending the switch /c to the command line we will get the number of open TCP sessions.

Established TCP sessions
We will still use the filter (in this case look for the string “estab” to find only lines containing the ESTABLISHED state) but together with /c the tool will only display the number of matching lines.
 
 
ipconfig /displaydns

Another short example could be the notice the number to locally cached dns records, see output above.

Find and count AD groups

The /c option could also be used to count all lines that a commands leaves as output. For example we would like to know the number of groups in Active Directory. By using a pipe to FIND /v “” /c we want to see all lines that does not (/v) match the string “”, i.e. all lines and then count it. By using /v “” we will grep every non-blank line, which could be useful if the output strings are very different. If you have used the Unix tool wc this would be quite like wc -l.

Another example with a command that displays a very large number of output line would be the Event Logger command line tool wevutil. If just wanting to know the number of different logs in modern Windows system we could pipe the names of several hundreds of log files into FIND /v “” /c.

A final example could be that we have a log file or similar with perhaps thousands of lines in total and we quickly want to know the amount of lines with certain key phrases.

TYPE C:\Windows\Schedlgu.txt | FIND /i “task failure” /c

It should be noted that the Unix tool wc has a number of Win32 ports available for free, but often it is most useful to know what could be accomplished from the builtin tools available on all Windows, even legacy versions.

18 thoughts on “Counting lines in Windows command prompt”

  1. Ali Awadh says:
    September 9, 2013 at 16:17

    Thank you. Thats really very useful.

    Reply
  2. Pablo Alejandro Hamann says:
    January 17, 2014 at 23:38

    Thanks! Very useful.

    One question, how can i store the value that FIND returns in a variable??
    I used the next command for count the number of images present in a Wim file:
    dism /Get-WimInfo /wimfile:M:\sources\install.wim /English | FIND /C /I “Index”

    But i need storage this number in a variable. So, i tried:
    SET IN=` dism /Get-WimInfo /wimfile:M:\sources\install.wim /English | FIND /C /I “Index” `

    but no luck. FIND answer: File not found: `

    Any ideas?

    Thanks!!!

    Reply
    1. Rickard Nobel says:
      January 17, 2014 at 23:46

      Hello Pablo,
      and thank you for your comment.

      You could use the FOR command to capture the line count into a variable. An example:

      dir C:\ | find /v /c “” > tmp1.txt

      FOR /f %%a IN (tmp1.txt) DO SET number=%%a

      ECHO %number%

      So just replace the line before the > tmp1.txt with your dism command and try it. The line count will here be placed into a variable called “number”, but that could of course be changed to what you find suitable.

      Regards, Rickard

      Reply
      1. donjoe says:
        October 16, 2015 at 11:23

        You don’t need FOR to get file contents into a variable, SET already knows how to do that:

        SET /P number=<tmp1.txt

        In fact, SET /P can be used to ECHO text without a newline char at the end (so you can continue to write to the same line afterwards):

        SET /P __DummyVar="And the winner is… "<NUL
        :: Insert here the algorithm to determine the winner and then…
        ECHO Peter Dinklage!

        😛

        Reply
      2. donjoe says:
        October 16, 2015 at 11:29

        … but what you _could_ use FOR /F to do is avoid writing the value to a file and just put it straight into the variable:

        FOR /F %%g IN (‘dir /a-d /s /b C:\ ^| find /v /c “”’) DO SET numberoffiles=%%g
        ECHO %numberoffiles%

        Reply
        1. Rickard Nobel says:
          October 16, 2015 at 12:20

          Hello Donjoe,

          thanks a lot for your comments. Always interesting with different tricks in classic CMD. 🙂

          I tend to use FOR for the reason of sometimes reading multiple lines and doing some manipulations of them, however the variant with SET /P number=<tmp1.txt when only getting one line/item was good!

          Best regards,
          Rickard

          Reply
  3. Pablo Alejandro Hamann says:
    January 20, 2014 at 18:21

    Thanks!!!

    Thats work for me.

    Greetings!!!

    Reply
  4. Da says:
    May 15, 2014 at 21:58

    Good stuff – Thanks so much – easy and very straight forward!

    Reply
  5. Richard says:
    September 9, 2015 at 18:19

    Invaluable, thank you!

    Reply
  6. Quan says:
    February 19, 2016 at 05:13

    I want to count all the lines after a command ran , however, I cannot get the integer value to echo. Could you please help?

    Example: ping google.com | find /v /c “”
    Result is: 11
    Then I want to echo There are 11 lines.

    Reply
    1. Rickard Nobel says:
      February 19, 2016 at 09:33

      Hello Quan,

      thank you for your question. If you create a batch file to run the commands you could do it like this:

      PING 192.168.100.77 | FIND /v /c “” > tmp1.txt
      SET /P lines=<tmp1.txt
      ECHO There are %lines% lines.

      Reply
      1. Quan says:
        February 22, 2016 at 04:38

        Hi Rickard,

        Thank you for your help.

        Reply
  7. JahanS says:
    February 26, 2016 at 16:39

    Not bad at all.
    I use Linux for most of my tasks but this command can prove to be useful if I am not running cygwin on the Windows box lol.

    Reply
  8. templer says:
    December 1, 2016 at 08:15

    Thanks, very good!

    Reply
  9. Prisha says:
    February 8, 2019 at 05:52

    Thanks very helpful , what i was looking for i got exactly here.

    Reply
  10. Johny Why says:
    September 11, 2021 at 01:31

    your /v “” seems redundant and unnecessary, if your output doesn’t contain any blank lines.

    Reply
    1. Br. Bill says:
      May 30, 2024 at 19:15

      “Blank Lines” in this instance is not an “empty line”. It just means a completely empty string. Since the newline is a string, then no line is truly empty or blank. It always gives a correct count of the file lines.
      I just created a file with 50 empty lines, and the 51st line says “The 51st line”. The result:

      > find /c /v “” 51_lines.txt

      ———- 51_LINES.TXT: 51

      Reply
  11. habibza says:
    April 1, 2023 at 17:33

    Great Post

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • The ARP Probe frame
  • The Active Directory VM Generation-ID, part 4
  • The Active Directory VM Generation-ID, part 3
  • The Active Directory VM Generation-ID, part 2
  • The Active Directory VM Generation-ID, part 1
  • AD-joined appliances cannot use Kerberos AES.
  • AD Trust: The other domain supports Kerberos AES – explained.

Contact:

©2021 RICKARD NOBEL AB