本帖最后由 went 于 2022-1-18 14:54 编辑
类似你顶楼的格式,输入a.txt,输出b.txt
如果有连续4个及以上文字相同视为重复,只保留第一个
ansi编码,试下吧,只是纯批效率有点慢- @echo off & cd /d "%~dp0"
- setlocal enabledelayedexpansion
- REM 文件设置
- set "in=a.txt"
- set "out=b.txt"
- REM 阈值设置 >=
- set "max=4"
- REM 比较并输出
- (
- set "num=1"
- for /f "usebackq delims=" %%i in ("%in%") do (
- title 处理第!num!行: %%i
- if not "!last_line!"=="" (
- REM 比较当前行与上一行
- call :GetMax "!last_line!" "%%i"
- REM 当前< echo last and set last
- if !#max_char! lss !max! echo !last! & set "last=%%i"
- set "last_max_char=!#max_char!!"
- ) else set "last=%%i"
- REM 保存当前行到上一行
- set "last_line=%%i" & set /a "num+=1"
- )
- REM 输出最后的last
- echo !last!
- )>"%out%"
- pause&exit
-
- REM 函数,获取两个字符串最大相同字符个数,%#max_char%
- :GetMax
- set "#s1=%~1" & set "#s2=%~2" & set /a "#i=0,#max_char=0"
- set "#s1=%#s1:"='%" & set "#s2=%#s2:"='%"
- :L1
- if "!#s1:~%#i%,1!"=="" goto :eof
- set "#j=1"
- :L2
- if "%#last%"=="!#s1:~%#i%,%#j%!" ( set /a "#i+=1" & goto :L1 )
- set "#last=!#s1:~%#i%,%#j%!"
- if not "!#s2:%#last%=!"=="%#s2%" if %#max_char% lss %#j% set /a "#max_char=#j"
- set /a "#j+=1" & goto :L2
复制代码
|