简体   繁体   English

如何使用批处理在文本文件中的某一行之后添加多行

[英]How do i add multiple lines after a certain line in a text file using batch

I need to edit a text file to insert multiple lines after a certain line with only features that come with windows 10我需要编辑一个文本文件以在特定行之后插入多行,仅具有 windows 10 附带的功能

example: Insert the lines foo and bar after line 4示例:在第 4 行之后插入行foobar

text file example before additions:添加前的文本文件示例:

line 1
line 2
line 3
line 4
line 5
line 6

text file example after additions:添加后的文本文件示例:

line 1
line 2
line 3
line 4
foo
bar
line 5
line 6

Read the file and write it back, insert the new lines at a specific lineno.读取文件并将其写回,在特定行号处插入新行。

@echo off

setlocal DisableDelayedExpansion
set randomline=4
set "lineno=0"
(
    FOR /F "delims=" %%L in ('findstr /n "^" sample.txt') do (
        set /a lineno+=1
        set "line=%%L"
        setlocal EnableDelayedExpansion        
        if "!lineno!"=="%randomline%" call :insertblock
        set "line=!line:*:=!"
        (echo(!line!)
        endlocal
    )
) > output.txt
exit /b

:insertblock
echo foo
echo bar
exit /b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM