在文本文件的中间添加多行

时间:2013-04-10 14:14:35

标签: cmd

确定 这里有一个文件txt.txt

line1
line2
add after this line
line4
etc
etc

和另一个文件addthis.txt 其中包含多行

我想在add after this line

之后添加所有这些行

我想知道的是我必须在this中做出的所有改变才能完成它?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

@ECHO OFF
SETLOCAL
SET /p info="enter info : "
:: read addafter line
(
FOR /f "delims=" %%i IN (poison1.txt) DO (
SET addafter=%%i
FOR /f "delims=" %%n IN (' findstr /n "^" txtfile.txt') DO (
SET line=%%n
SETLOCAL ENABLEDELAYEDEXPANSION
SET line=!line:*:=!
ECHO(!line!
IF "!line!"=="!addafter!" TYPE your_multiline_addition.txt
ENDLOCAL
)
)
)>newfile.txt

FC newfile.txt txtfile.txt

GOTO :eof

只需要TYPE your_multiline_addition.txt而不是ECHO(%input%

(你也可以抛弃你的输入)

OR

你可以

TYPE "%info%"

如果要输入包含附加内容的文件的名称


测试数据和结果:

======== poison1.txt ===========
"A line !of! ] many < & >var*ied %poison ^ char;ac(ters) | like "," a\nd+so=on"
======== extra_lines.txt ===========
extra line one
extra line two
extra line three
======== txtfile.txt ===========
line one 
line two 
line three
A line !of! ] many < & >var*ied %poison ^ char;ac(ters) | like "," a\nd+so=on

line 5 was blank - this is line FIVE
"A line !of! ] many < & >var*ied %poison ^ char;ac(ters) | like "," a\nd+so=on"

line 8 was blank - this is line NINE
======== newfile.txt ===========
line one 
line two 
line three
A line !of! ] many < & >var*ied %poison ^ char;ac(ters) | like "," a\nd+so=on

line 5 was blank - this is line FIVE
"A line !of! ] many < & >var*ied %poison ^ char;ac(ters) | like "," a\nd+so=on"
extra line one
extra line two
extra line three

line 8 was blank - this is line NINE