What is the Windows equivalent of the Linux/Unix command wc -l?
Basically, how do you count the number of lines output from a command on the Windows command line?
What is the Windows equivalent of the Linux/Unix command wc -l?
Basically, how do you count the number of lines output from a command on the Windows command line?
The Linux/Unix "line count" command, wc -l, has a Windows equivalent find /c /v "".
According to Raymond Chen of the The Old New Thing, this functions as such since
It is a special quirk of the
findcommand that the null string is treated as never matching.
The inverted (/v) count (/c) thus effectively counts all the lines;
hence, the line count.
To count the number of modified files in a subversion working copy:
svn status -q | find /c /v ""
Such a command can be used to mark a build as "dirty" if the count is not 0, i.e. there are uncommitted changes in the working copy.
To obtain a line count of all your Java files:
(for /r %f in (*.java) do @type "%f") | find /c /v ""
The command find /c /v "" can also be added to a batch file if required.
Remember to duplicate the % characters (%%f) in batch files.
PowerShell
A working PowerShell equivalent is Measure-Object -line — some additional formatting is required though, e.g. a directory listing for simplicity:
(ls | Measure-Object -line).Lines
ls | find /c /v """"""
– pjpscriv
Nov 03 '22 at 02:31
(ls | Measure-Object -line).Lines, exactly what I was looking for ;), and @pjpscriv, for me ls | find /c /v """""" does not work since it does not count only files but also all empty lines and tables header around, while Measure-Object method actually gives me 28 where I have 28 files, findstr method gives me 37 (file lines + extra lines).
– gluttony
Aug 11 '23 at 09:32
In PowerShell, to obtain a line count of all your java files:
type *.java | Measure-Object -line
Microsoft.PowerShell_profile.ps1 file: function wc () { Measure-Object -c -l -w } (it did not work as expected.)
– MarkHu
Jul 14 '23 at 22:16
findstr /n /r .
This is a limited regex, so /n shows the line number of the match, and "." is match any char.
wc(well,wc.exe...) http://gnuwin32.sourceforge.net/ – ivanivan Jan 15 '19 at 19:03