统计文本文件中重复行的批处理,统计重复内容所在行号。
感觉方法有点乱,但没想出更好的处理方法了。- ::---------以下代码用于生成测试文本---------
- @echo off
- del testFileName.txt>nul 2>nul
- echo 正在生成测试文本!
- for /l %%a in (1,1,100) do (
- >>testFileName.txt call echo %%random:~,2%%
- )
- cls
- ::---------以上代码用于生成测试文本---------
-
- @ECHO OFF
- SETLOCAL ENABLEDELAYEDEXPANSION
- mode con: cols=45 lines=4
- title CodeBy 0428_hai CMD@XP 2011-04-26
- set fileName=testFileName.txt
- del temp>nul 2>nul
- del temp.txt>nul 2>nul
- del result.txt>nul 2>nul
- if not exist "%fileName%" set sec=0 & goto :end
-
- for /f "delims=" %%a in (%fileName%) do (
- set /a sn+=1
- >>temp echo !sn! %%a
- )
- copy temp _%fileName%>nul
- call :times %time:~,-3% sec_Start
-
- :main
- if not exist temp goto :end
- for /f "tokens=1* delims= " %%a in (temp) do (
- call :count !sn!
- set sn=
- for /f "skip=1 tokens=1* delims= " %%A in (temp) do (
- cls
- echo 大约还需要进行 !count! 次运算!
- echo 当前时间:!time!
- call :times !time:~,-3! sec_Cur
- set /a sec=!sec_Cur!-!sec_Start!
- echo 已用时间:!sec! 秒
- set /a count-=1
- if "%%b" == "%%B" (
- set var=!var!%%A
- ) else (
- >>temp.txt echo %%A %%B
- set /a sn+=1
- )
- )
- cls
- if defined var >>result.txt echo 第 %%a !var!行出现了%%b!
- set var=
- del temp>nul 2>nul
- ren temp.txt temp>nul 2>nul
- goto :main
- )
-
- :end
- if exist result.txt (
- echo 操作已完成,查询结果保存在result.txt文件中!
- start "" result.txt
- ) else (
- echo 在文件中没有查到重复行!
- )
- echo 使用时间:!sec! 秒!
- pause>nul
- exit
-
- :count
- set /a count=%1-1
- set /a count=%1*%count%/2
- goto :eof
-
- :times
- for /f "tokens=1,2,3 delims=:" %%a in ("%1") do (
- set /a sec=1%%c%%100
- set /a min=1%%b%%100
- set /a %2=%%a*3600+!min!*60+!sec!
- )
- goto :eof
复制代码
|