![](/img/trans.png)
[英]How to add line numbers to a text file using batch file on WindowsXP
[英]How to add line numbers to a text file from a batch file (Windows)
如何从批处理文件/命令提示符向文本文件添加行号?
例如
1 line1
2 line2
etc
我能得到的最接近的是,这不起作用:
@echo off
set file=%1
set x=1
for /f "delims=|" %%i in (%file%) do (
echo %x% %%i
set /a x=%x%+1
)
for
循环中的set
不起作用(因为我们处于糟糕的DOS中)。
通过call
另一个批处理文件来替换该set
来进行x
的增量和设置也不起作用。
附录
好的,添加@indiv建议的修复,我们得到了这个(看起来确实有效):
@echo off
set file=%1
set x=1
setlocal EnableDelayedExpansion
for /f "delims=|" %%i in (%file%) do (
echo !x! %%i
set /a x=!x!+1
)
太过复杂了,让我们知道这一点。 对于Windows XP及更高版本(我们需要findstr),以下是命令行所需的全部内容,或者批量添加行号到OP所需的输入文件...
type "in.txt"|findstr/n ^^ > "out.txt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.