我需要根据循环For中的行号将一行提取到一个文件中。实际上,我从第一个FOR命令获得第7行,到目前为止一直很好,我必须将此行读入myfile.txt。读取操作在第二个FOR循环中执行;此步骤不起作用,因为我输出'result.txt'为空
for /f "tokens=1* delims=:" %%a in (lines.txt) do set line=%%a& goto breakFor "%line%"
:breakFor
echo.%line%
for /f "tokens=*" %%a in ('findstr /n .* "myfile.txt"') do if "%%a"=="%1" set line=%%i
echo.%line%>result.txt
这是myfile.txt和lines.txt
的一部分lines.txt
7:I=output7
515:I=output515
837:I=output837
851:I=output851
myfile.txt is made of lines of text (I1,I2,I3,...)
I1=some text....
I2=text...
你能告诉我为什么并帮助解决这个问题吗? 感谢
答案 0 :(得分:2)
下面的批处理文件执行以下操作:
for /f "tokens=1* delims=:" %%a in (lines.txt) do set line=%%a& goto breakFor
:breakFor
echo.%line%
for /f "tokens=1* delims=:" %%a in ('findstr /n "^" "myfile.txt" ^| findstr "^%line%:') do set line=%%b
echo.%line%>result.txt
这是你想要的吗?我更希望这样做:
for /f "tokens=1* delims=:" %%a in (lines.txt) do set /A line=%%a-1& goto breakA
:breakA
echo.%line%
for /f "skip=%line% tokens=1* delims=:" %%a in ('findstr /n "^" "myfile.txt"') do set line=%%b& goto breakB
:breakB
echo.%line%>result.txt
答案 1 :(得分:1)
试试看看输出:
@echo off &setlocal
set "line=test"
goto breakFor "%line%"
:breakFor
echo "%~1"