Sab*_*ina 5 command-line window
在 Linux 中,我可以使用命令轻松查看旁边有行号的任何文件cat -n。
sh-4.4$ cat test
line 1
line 2
line 3
sh-4.4$
sh-4.4$ cat test -n
1 line 1
2 line 2
3 line 3
sh-4.4$
Run Code Online (Sandbox Code Playgroud)
那么 Windows 呢?是否有类似的命令产生类似的输出?
type是查看文件内容的 Windows 命令之一。
C:\>type test.txt
line 1
line 2
line 3
C:\>
Run Code Online (Sandbox Code Playgroud)
然而,其中没有任何-n选项。
C:\>type /?
Displays the contents of a text file or files.
TYPE [drive:][path]filename
C:\>
Run Code Online (Sandbox Code Playgroud)
这就是我在 Windows 中所期待的
C:\>(windows command to view and print line number) test.txt
1 line 1
2 line 2
3 line 3
C:\>
Run Code Online (Sandbox Code Playgroud)
在不安装第三方工具的情况下,可以使用 Powershell 来完成此操作,但脚本编写有点复杂,如下所示。该脚本的优点在于它可以与任何产生输出的 DOS 命令一起使用。
$i = 1; 猫粮.txt | % {$i++;"$($i-1) `t $_"}
注意:Powershell 有该cat命令,如脚本中所示,但缺少行编号。它是 PowerShell 命令的别名get-content。
这是输出:
1 Apples
2 Bananas
3 Carrots
Run Code Online (Sandbox Code Playgroud)
dir这是一个通过简单地在脚本中执行来列出目录的示例:
$i = 1; 目录 | % {$i++;"$($i-1) `t $_"}
这是输出:
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
Run Code Online (Sandbox Code Playgroud)
如果您希望行号从 0 开始,则设置 $i = 0
cat或者,您可以在 Windows 中安装第 3 方程序。UNIX 命令有许多到 Windows 的端口,例如Cygwin和GnuWin32。
安装 Cygwin 的基本安装后:
C:\cygwin64\bin\cat -n food.txt
输出:
1 apple
2 banana
3 bread
4 cake
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21456 次 |
| 最近记录: |