0

There is a text file with an unknown number of rows like this:

$ cat test1
123
abc
456
def
...
def
123
456
def
abc
456
123

Using the windows console tools it's necessary to determine the number of rows '123'.

Ulugbek
  • 3
  • 4
  • 2
    Possible duplicate of [How to find the number of occurrences of a string in file using windows command line?](http://stackoverflow.com/questions/9307187/how-to-find-the-number-of-occurrences-of-a-string-in-file-using-windows-command) – Alex K. Nov 16 '16 at 11:26
  • I don't think they're looking for the number of times 123 appears in the file; I think they're looking for the number of lines in the file, like the Windows equivalent of `wc -l` (since I see you're using `cat`, I'm assuming you have Gnu Tools or something installed). – SomethingDark Nov 16 '16 at 12:15
  • FIND command with the /V and /C options. – Squashman Nov 16 '16 at 13:20

1 Answers1

0

If it is the complete content of the line

findstr "^123$" test.txt |find /C "123"

If you want the result be put in a var on the cmdline

For /f %A in ('findstr "^123$" test.txt ^|find /C "123"') Do set Var=%A

If you want the result be put in a var inside a batch

For /f %%A in ('findstr "^123$" test.txt ^|find /C "123"') Do set Var=%%A
  • Cool! It works! But what if 123 is the last row in file? – Ulugbek Nov 17 '16 at 10:31
  • If that last line has no crlf that wouldn't be counted. Maybe you could append a crlf by `>>test.txt echo:` –  Nov 17 '16 at 10:41