我有一个看起来像这样的文件
0181830207709014000015576526S00000140000014000001242017
0052500056661095000015576527S00000950000095000001242017
0157020294103030000015576528S00000300000030000001242017
2397870060994031500015576529S00000315000031500001242017
2466260219154015000015576530S00000150000015000001242017
我需要插入一个名为&#34的特定文本;插入一个新行"在line2之后,将输出重定向到具有不同文件名的新文件。我如何使用批量编程来解决这个问题。
答案 0 :(得分:0)
使用Findstr /N
对文件行进行编号
> findstr /N ".*" test.txt
1:0181830207709014000015576526S00000140000014000001242017
2:0052500056661095000015576527S00000950000095000001242017
3:0157020294103030000015576528S00000300000030000001242017
4:2397870060994031500015576529S00000315000031500001242017
5:2466260219154015000015576530S00000150000015000001242017
用For /f
循环来解析输出,在冒号处将%%A
分隔为数字,将%%B
分隔为行内容。如果数字为2
,请插入额外的行。
@Echo off
Set "File=Test.txt"
Set "NewFile=TestNew.txt"
(
For /f "Tokens=1*Delims=:" %%A in (
'Findstr /N ".*" "%File%"'
) Do If %%A Equ 2 (
Echo:insert me a new line before
Echo:%%B
Echo:insert me a new line below
) Else (
Echo:%%B
)
) >"%NewFile%"
EDIT 改变了后续读者的逻辑。样本输出:
> type TestNew.txt
0181830207709014000015576526S00000140000014000001242017
insert me a new line before
0052500056661095000015576527S00000950000095000001242017
insert me a new line below
0157020294103030000015576528S00000300000030000001242017
2397870060994031500015576529S00000315000031500001242017
2466260219154015000015576530S00000150000015000001242017