1

I am trying to count lines in a file following the answer here, e.g. using find /C, but all I see in console is:

find: '/C': No such file or directory
find: 'A': No such file or directory

I have a simple "TEST.txt" file with three As and three Bs, all in new lines:

$ type TEST.txt
A
A
A
B
B
B

findstr seems to work, but it cannot return counts:

$ type TEST.txt | findstr "A" 
A                             
A                             
A   

find does not work:

$ type TEST.txt | find "A"
find: 'A': No such file or directory

What am I missing?

PS: The whole example from 1 does not work as well:

$ findstr /R /N "^" TEST.txt | find /C ":"
find: '/C': No such file or directory
find: ':': No such file or directory

Edit:

As it might be important to understand what is happening: I am using a ConEmu terminal.

m-dz
  • 2,342
  • 17
  • 29
  • Are you invoking `find` from `cmd.exe` or `powershell.exe`? – Mathias R. Jessen Jul 10 '17 at 15:22
  • TEST.txt is an existing file? Your exact command `findstr /R /N "^" TEST.txt | find /C ":"` works for me, on a file where I count occurences of "A". – Rizky Fakkel Jul 10 '17 at 15:22
  • I used `cmd.exe` by the way. His error looks like something UNIX would return. `powershell.exe` would return `FIND:Parameter format not correct` because syntax is different. – Rizky Fakkel Jul 10 '17 at 15:23
  • As far as I understand I am using `cmd.exe` (command `/cmd {Shells::cmd (Admin)}` in FreeCommander XE)... – m-dz Jul 10 '17 at 15:41
  • And TEST.txt is an existing file... `findstr /R /N "^" TEST.txt | find /C ":"` still returns `find: '/C': No such file or directory` and `find: ':': No such file or directory` for me. – m-dz Jul 10 '17 at 15:42
  • Also, I am using ConEmu as the terminal, but I believe this should not change the `cmd.exe` behavior... – m-dz Jul 10 '17 at 15:43
  • As stated in the comment under the @RizkyFakkel answer: it was not the ConEmu itself, but Rtools has apparently a "linux style" find.exe in its folder. Full path to find.exe works, as is the linux's `wc -l`. – m-dz Jul 10 '17 at 23:26

2 Answers2

6

Do you have the ConEmu binary directory containing find.exe in the PATH before the Windows directory containing find.exe?

If you cannot change the PATH variable, you can specify the full path to the Windows find with %windir%\System32\find.exe.

lit
  • 14,456
  • 10
  • 65
  • 119
  • RTools did this to me, never expected another find.exe would jump ahead of %windir%\system32 in the PATH variable. – AjimOthy Oct 11 '17 at 01:46
1

You are using Windows' commands for cmd.exe on a UNIX shell. find on UNIX is different from find on Windows' shell.

To count lines in a file in linux, use wc -l /path/to/file.

If you want to use the Windows CLI, open up cmd.exe and use the answer you linked to:

findstr /R /N "^" <FILENAME> | find /C ":"

If you want to go a step further and add more options, take a look at this link

Rizky Fakkel
  • 8,703
  • 1
  • 14
  • 20
  • I am even more puzzled now... I am opening a `cmd.exe` using Windows start menu, `cmd.exe` opens in ConEmu, which I believed was only a "handy windows terminal" allowing for copying, pasting etc., and `findstr /R /N "^" TEST.txt | find /C ":"` does NOT work, but `wc -l TEST.txt` does... – m-dz Jul 10 '17 at 15:46
  • Do you have the ConEmu binary directory containing find.exe in the PATH before the Windows directory containing find.exe? I admit that I do not know ConEmu, but just a guess. – lit Jul 10 '17 at 19:37
  • 1
    Good guess @lit! It was not the ConEmu itself, but Rtools had find.exe, with the full path to Windows one the `findstr /R /N "^" TEST.txt | C:\Windows\System32\find.exe /C ":"` worked like a charm. `where find` did the job here. Please post this as an answer and I will accept it. – m-dz Jul 10 '17 at 23:24
  • Rizky Fakkel, +1 for you for adding a bit of background to the problem! I am accepting @lit answer as a solution as it directly solved it, but your input is also valuable here. – m-dz Aug 29 '17 at 09:55