level 1
findstr 中,^1 的意思是取行首为 1 的行,当然不对了。。。。取指定行的办法很多,举几个相互之间差异较大的例子
set /p n=要取的行数
::以下不考虑用户输入的合法性,而且计算行数时包括空行
findstr /n .* a.txt|findstr /b %n%:
::纯 findstr 法,缺点是效率不够高,而且带行号
for /f "tokens=1* delims==" %%a in ('findstr /n .* a.txt') do if %%a==%n% echo %%a
::最常用的办法,较准确,但是不适合对付大文件
for /f "skip=%n% tokens=1* delims==" %%a in ('echo\^&findstr /n .* a.txt') do if not defined echo set echo=%%b
echo;%echo%
::另一种办法,但是同样不适合对付大文件
(for /l %%a in (1 1 %n%) do set /p=
set /p str=)<a.txt
echo;%str%
::适用于处理大文件,但支持的行字数上限只有 1023 字节
more +1 a.txt>tmp.txt
findstr /v /x /g:tmp.txt a.txt
::另类思路,适用于每行内容不一致的时候,但是缺点也是显而易见得多。。。。。
2011年10月18日 13点10分