当前位置:DOS资源站资料中心批处理教程 → 读取文件中的某几行到另外一个文件

读取文件中的某几行到另外一个文件

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2008-5-31 21:38:25

读取文件test1.txt 中的第1,3,4,6,7行到test2.txt
test1 内容
port a 1
port b 2
port c 3
port d 4
port e 5
port f 6
port g 7
port h 8
port i 9

@echo off
for /f "delims=" %%i in ('findstr "1 3 4 6 7" a.txt') do (set num=%%i
>>test2.txt call,echo %%num%%
)
pause

如果test1中的文件内容是这样

SET Port1=0
SET Port2=1
SET Port2=low

SET Port1=0
SET Port2=0


SET Port1=0
SET Port2=0


SET Port1=0
SET Port2=0
SET Port3=0
SET Port4=0
SET Port5=1
SET Port5=full

SET Port6=1
SET Port6=full

SET Port1=0
SET Port2=0


SET Port1=0
SET Port2=0


SET Port1=1
SET Port1=full

SET Port2=0


SET Port1=1
SET Port1=full

SET Port2=0
SET Port3=0
SET Port4=0
SET Port5=0
SET Port6=0


@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%i in (test1.txt) do (
     set str=%%i
     if defined str (
        set /a n+=1
        set _!n!=!str!
     )
)
for %%i in (1 3 4 6 7) do echo !_%%i!>>test2.txt
pause>nul