
CODE: [Copy to clipboard]::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: find5line.bat -- 从文本查找关键字并显示其下5行
:: by: 无奈何 - 2005-10-1 - 联系:wunaihe@gmail.com
:: 用法:直接运行,显示输出。
:: 说明: 适用环境 Win2000 以上,实测 WinXP。
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal ENABLEDELAYEDEXPANSION
set /p str=请输入关键字:
set /p name=输入文件名:
set m=1
::获取关键字所在行
for /f "tokens=1,2* delims=:" %%a in ('findstr /n /C:"!str!" !name!') do @(
set line=%%a
set /a line-=1
set n=1
echo.
echo 查找到第 !m! 个匹配
set /a m+=1
::显示关键字及其下5行
for /f "delims=" %%i in ('more +!line! !name!') do (
if !n! LEQ 5 echo %%i
set /a n+=1
)
)
endlocal
CODE: [Copy to clipboard]思路很好,只是不支持多行匹配。@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1 delims=[]" %%i in ('find /n %1 %2^|more +2') do (
set min=%%i
set /a max=!min! + 8
)
for /f "tokens=1* delims=[]" %%i in ('find /n /v "" %2^|more +2') do (
if %%i geq !min! (if %%i leq !max! (
if "%%j"=="" (echo.) else (echo.%%j)))
)
endlocal
CODE: [Copy to clipboard][ Last edited by 无奈何 on 2005-10-3 at 23:46 ]@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1 delims=[]" %%i in ('find /n %1 %2^|more +2') do (
set min=%%i
set /a max=!min! + 8
echo.
for /f "tokens=1* delims=[]" %%i in ('find /n /v "" %2^|more +2') do (
if %%i geq !min! (if %%i leq !max! (
if "%%j"=="" (echo.) else (echo.%%j)))
))
endlocal
| 欢迎光临 中国DOS联盟论坛 (http://cndos.fam.cx/forum/) | Powered by Discuz! 2.5 |