Windows equivalent to Linux `cat -n`?
Without installing a 3rd party tools, this can be done using Powershell, but there is a little bit of scripting complexity as seen below. What is nice about this script is that it will work with any DOS command that produces output.
$i = 1; cat food.txt | % {$i++;"$($i-1) `t $_"}
Note: Powershell has the cat
command, as seen in the script, but it lacks line numbering. It is an alias for the PowerShell command get-content
.
Here is the output:
1 Apples
2 Bananas
3 Carrots
Here is an example with a directory listing by simply executing dir
in the script:
$i = 1; dir | % {$i++;"$($i-1) `t $_"}
Here is the output:
1 backgrounds
2 boot
3 inetpub
4 PerfLogs
5 Program Files
6 Program Files (x86)
7 Riot Games
8 Users
9 Windows
10 Reflect_Install.log
If you want line numbering to start at 0, then set $i = 0
Alternatively, you can install 3rd party cat
program in Windows. There are many ports of UNIX commands to Windows, such as Cygwin and GnuWin32.
After installing Cygwin's base install:
C:\cygwin64\bin\cat -n food.txt
Outputs:
1 apple
2 banana
3 bread
4 cake