使用批处理删除文本文件中的某一行

时间:2015-11-10 05:02:10

标签: batch-file

我有一个txt文件。我想删除第4行和第5行。

之前

Line1  
Line2  
Line3  
Line4 (need to delete)  
Line5 (need to delete)  
Line6  

之后

Line1  
Line2  
Line3  
Line6

2 个答案:

答案 0 :(得分:1)

@echo off
setlocal EnableDelayedExpansion

call :CopyLines < input.txt > output.txt
move /Y output.txt input.txt
goto :EOF


:CopyLines

rem Copy lines 1, 2 and 3
for /L %%i in (1,1,3) do (
   set "line="
   set /P "line="
   echo(!line!
)

rem Omit lines 4 and 5
for /L %%i in (4,1,5) do set /P "line="

rem Copy the rest
findstr "^"

exit /B

答案 1 :(得分:0)

尝试下面的代码,它可以完全符合您的要求:

@echo off
cls
setlocal EnableDelayedExpansion
set /a count=0
if exist neww.txt (del /s /q neww.txt) 

for /f "tokens=*" %%a in (onee.txt) do (
set /a count+=1

if !count! EQU 4 (
echo do nothing
)else if !count! EQU 5 (
echo do nothing
)else (
echo %%a>>neww.txt
)
)

move /y neww.txt onee.txt