@Echo Off&Setlocal Enabledelayedexpansion
For /f "tokens=1* delims=:" %%i in ('Type a.txt^|Findstr /n "问问"') do (
Set n=&Set /a n=%%i+1
Echo %%j>>b.txt
For /f "tokens=1* delims=:" %%a in ('Type a.txt^|Findstr /n "."') do (If %%a==!n! Echo %%b>>b.txt)
)
Pause
可以处理a.txt中包含多个 搜索结果.例如 搜索结果包含多个"问问".
@echo off
cd /d %~dp0
rem 设置文件
set f=abc.txt
rem 设置搜索内容
set t=问问
rem 设置输出文件
set n=NewFile.txt
for /f "tokens=1,2 delims=:" %%a in ('findstr /n "%t%" "%f%"') do (
set h=%%a
pause
echo %%b>"%n%"
goto next
)
:next
for /f "skip=%h% delims=" %%c in ('type "%f%"') do (
echo %%c>>"%n%"
pause
exit
)
@echo off
setlocal enabledelayedexpansion
for /f "delims=: tokens=1,2" %%i in ('type a.txt ^| findstr/n "问问"') do (
set /a a=%%i+1
echo %%j>>b.txt
)
for /f "delims=: tokens=1,2" %%a in ('type a.txt ^| findstr/n .*') do (
if %%a==!a! echo %%b>>b.txt
)
grep -A 1 "问问" a.txt >> b.txt