What is the equivalent of Bash's cat -n in PowerShell?
You potentially abuse Select-String for that:
Select-String -Pattern .* -Path .\foo.txt | select LineNumber, Line
Example output:
LineNumber Line
---------- ----
1 a
2
3 b
4
5 c
I want to cat a file and output the line number of each line it outputs.
Use the following command:
$counter = 0; get-content .\test.txt | % { $counter++; write-host "`t$counter` $_" }
As pointed out in the comments:
- It may be better to use
write-output
instead ofwrite-host
as this allows further processing of the output. echo
is an alias forwrite-output
So the above command becomes:
$counter = 0; get-content .\test.txt | % { $counter++; echo "`t$counter` $_" }
Example output:
> type test.txt
foo
//approved
bar
// approved
foo
/*
approved
*/
bar
> $counter = 0; get-content .\test.txt | % { $counter++; echo "`t$counter` $_" }
1 foo
2 //approved
3 bar
4 // approved
5 foo
6 /*
7 approved
8 */
9 bar
>
Example output from Cygwin cat -n
for comparison:
$ cat -n test.txt
1 foo
2 //approved
3 bar
4 // approved
5 foo
6 /*
7 approved
8 */
9 bar
$