本帖最后由 CrLf 于 2013-2-7 21:17 编辑
忘了保留,已修改:- @echo off
- sort a.txt>tmp.txt
- set "key=0 7 2 5 3"
- (for %%a in (0 7 2 5 3) do (
- findstr /b "%%a" tmp.txt
- )
- findstr /vb "[%key: =%]" tmp.txt
- )>b.txt
- pause
复制代码 或用这个更通用的,但会多生成一个临时文件:- @echo off
- sort a.txt>tmp.txt
- set "key=0 7 2 5 3"
- (for %%a in (0 7 2 5 3) do (
- findstr /b "%%a" tmp.txt
- )
- )>tmp2.txt
- copy /y tmp2.txt b.txt>nul
- findstr /vbg:tmp2.txt tmp.txt>>b.txt
- pause
复制代码 再来个常见思路的,速度会快一些:- @echo off
- set "文件名=b.txt"
- set "key=0 7 2 5 3">"%文件名%"
- (for /f "delims="eol^= %%a in ('sort a.txt') do (
- set "str=%%a"
- setlocal enabledelayedexpansion
- for %%b in (%key%) do (
- if "!str:~,1!"=="%%b" (
- echo !str!>>"%文件名%"
- set "str="
- )
- )
- if defined str echo;!str!
- endlocal
- ))>tmp.txt
- type tmp.txt>>"%文件名%"
- pause
复制代码
|