濮阳杆衣贸易有限公司

主頁 > 知識庫 > 統(tǒng)計重復(fù)次數(shù)并排序的批處理

統(tǒng)計重復(fù)次數(shù)并排序的批處理

熱門標(biāo)簽:四川防封電銷卡套餐 浙江語音外呼系統(tǒng)招商 95專業(yè)外呼線路是怎么回事 谷歌地圖標(biāo)注錯位 外呼獲客系統(tǒng) 無錫極信防封電銷卡價格 沈陽電銷 用電話機器人取代人工 aa專業(yè)電話外呼系統(tǒng)
復(fù)制代碼 代碼如下:

@echo off
:: 目的:
:: SearchNet.TXT中每行只有一個數(shù),統(tǒng)計每個數(shù)的重復(fù)次數(shù),并按照重復(fù)次數(shù)由高到低排序
:: 思路:
:: 先用sort把所有的數(shù)排序,然后統(tǒng)計重復(fù)次數(shù),以 數(shù)+重復(fù)次數(shù) 的格式寫入臨時文件tmp2.txt;
:: 提取重復(fù)次數(shù)的數(shù)字,以該數(shù)字為長度建立以該數(shù)字命名的文件,用dir來把文件名(即重復(fù)次數(shù))排序,寫入tmp3.txt;
:: 按行提取tmp3.txt中的內(nèi)容,然后在tmp2.txt中查找與tmp3.txt中匹配的記錄,寫入結(jié)果;
:: 此方案會產(chǎn)生大量的臨時文件,但是效率比較高
::
::
set num=-1
sortSearchNet.TXT>tmp1.txt
cd.>tmp2.txt
cd.>tmp3.txt
cd.>result.txt
:: 統(tǒng)計重復(fù)次數(shù)
setlocal enabledelayedexpansion
for /f %%i in (tmp1.txt) do (
set /a num+=1
set second=!first!
set first=%%i
if not "!second!"=="" if !second! neq !first! (>>tmp2.txt echo !second! !num!set num=0)
)
>>tmp2.txt echo %first% %num%
:: 對重復(fù)次數(shù)排序
md tmp pushd tmp
for /f "tokens=2" %%i in (..\tmp2.txt) do (
cd.>%%i
for /l %%j in (1,1,%%i) do echo.>>%%i
)
>..\tmp3.txt dir /o-s /b
:: 按重復(fù)次數(shù)提取記錄
for /f %%i in (..\tmp3.txt) do (
>>..\result.txt findstr " %%i$" ..\tmp2.txt
)
popd rd /q /s tmp
del tmp1.txt tmp2.txt tmp3.txt
start result.txt
goto :eof
關(guān)于統(tǒng)計字符出現(xiàn)個數(shù)的其他方案(都不生成臨時文件)
@echo off
:: 統(tǒng)計每個字符出現(xiàn)的次數(shù),并求出出現(xiàn)次數(shù)最多的字符
:: 思路:
:: 通過提取每個位上的字符,賦予統(tǒng)一以 字符: 開頭的某些動態(tài)變量,
:: 如果變量名相同,則自加一次,然后,通過 set 字符:命令一次性提取
:: 所有以 字符: 開頭的動態(tài)變量,交給 for 語句來處理。set 用得很巧妙
:: 無須生成臨時文件,并按照字母升序排列
::
::
::
setlocal ENABLEDELAYEDEXPANSION
set str=adadfdfseffserfefsefseetsdmg
set /a m=0,n=0,l=0
call :loop
:: 以下是求出現(xiàn)次數(shù)最多的字符
for /f "tokens=1,2 delims==" %%i in ('set 字符:') do (
echo %%i=%%j
if %%j GTR !l! set l=%%j set m=%%i
)
echo.出現(xiàn)次數(shù)最多的%m%=%l%
pause
goto :EOF
:loop
call set m=%%str:~%n%,1%%
if not defined m goto :EOF
set /a "字符:%m%+=1"
set /a n+=1
goto loop
@echo off
:: 統(tǒng)計字符出現(xiàn)次數(shù)
:: 思路:
::    先把字符串拆解為以空格分隔的單字符組成的字符串,
:: 然后用for語句來探測每個字符在串中出現(xiàn)的次數(shù)
:: 此方法無須生成臨時文件,并按照在字符串中出現(xiàn)的
:: 先后順序顯示
::
::
::
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdg
rem 拆解字符串
:analyze
set str_tmp=%str_tmp% %str:~0,1%
set str=%str:~1%
if not "%str%" == "" goto analyze
rem
for %%i in (%str_tmp%) do call :exclude %%i
pause
exit
:exclude
for %%i in (%counted%) do if "%1"=="%%i" goto :eof
set counted=%counted% %1
call :count %1
goto :eof
:count
for %%i in (%str_tmp%) do if "%1"=="%%i" set /a %1+=1
echo %1 !%1!
goto :eof
@echo off
:: 統(tǒng)計字符出現(xiàn)的次數(shù)
:: 思路:
:: 拆解字符串,以空格分隔組成新字符串
:: 通過 shift 來call 不同的參數(shù),并用
:: set 來命名變量,變量名具有統(tǒng)一的開頭
:: 最后通過 set 來顯示這些變量
::
::
::
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdg
:loop
set str_tmp=%str_tmp% %str:~0,1% set str=%str:~1%
if not "%str%" == "" goto loop
call :start %str_tmp%
set .
echo 出現(xiàn)次數(shù)最多的:%max%=%maxN%
pause
exit
:start
if [%1]==[] ( goto :eof ) else ( set /a .%1+=1 )
if !.%1! GTR !maxN! set maxN=!.%1! set max=.%1
shift
goto :start
@echo off
:: 綜合以上方案,最簡潔的代碼如下
::
::
setlocal EnableDelayedExpansion
set str=adadfdfseffserfefsefseetsdgadadfdfseffserfefsefseetsdga
:loop
set str$=%str$% %str:~0,1%set str=%str:~1%
if not "%str%" == "" goto loop
for %%n in (%str$%) do (
set /a .%%n+=1
if !.%%n! GTR !maxN! set maxN=!.%%n!set max=%%n)
set .
echo 出現(xiàn)次數(shù)最多的:%max%=%maxN%
pause
exit
@echo offsetlocal
:: sort之后,通過比較這一次取到的內(nèi)容和上一次的內(nèi)容是否相等來統(tǒng)計重復(fù)次數(shù)
:: 如何同時保存本次和上次的內(nèi)容需要很大的技巧
:: 注意要把次數(shù)的初值設(shè)置為1,for語句的后括號之后不能緊跟跳出語句
::
::
set /a n=1
for /f %%a in ('type 1.txt^|sort') do (
call :pp %%a
)
:pp
if not defined bb goto b
if "%bb%"=="%1" (set /a n+=1) else (>>ko.txt echo %bb% %n%次set /a n=1)
:b
set bb=%1
goto :eof
@echo offsetlocal enabledelayedexpansion
:: 帶排序功能的代碼
:: 用 for /l 來控制每次 findstr 的字符長度,
:: 然后把同一長度的用 sort 來排序,從而突破了
:: sort 只能按字符位大小來排序這一限制
::
::
set a=[0-9]
for /l %%a in (1,1,3) do (
call :pp !a!
set a=!a![0-9]
)
goto c
:pp
for /f %%x in ('findstr "^%1$" aa.txt^|sort') do @echo %%x >>dd.txt
goto :eof
:c
set /a n=1
for /f %%a in ('type dd.txt') do (
call :pp %%a
)
:pp
if not defined bb goto b
if "%bb%"=="%1" (set /a n+=1) else (>>ko.txt echo %bb% %n%次set /a n=1)
:b
set bb=%1
goto :eof

標(biāo)簽:廣安 興安盟 連云港 南平 常德 迪慶 青島 煙臺

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《統(tǒng)計重復(fù)次數(shù)并排序的批處理》,本文關(guān)鍵詞  統(tǒng)計,重復(fù),次數(shù),并,排序,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《統(tǒng)計重復(fù)次數(shù)并排序的批處理》相關(guān)的同類信息!
  • 本頁收集關(guān)于統(tǒng)計重復(fù)次數(shù)并排序的批處理的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    马边| 嘉荫县| 桐梓县| 香港 | 宣汉县| 鄂伦春自治旗| 沙洋县| 阳朔县| 顺昌县| 囊谦县| 桐梓县| 延寿县| 武功县| 通化县| 屏山县| 财经| 太仓市| 连江县| 岳池县| 同心县| 曲阳县| 双城市| 淮阳县| 合山市| 阆中市| 黎平县| 康定县| 塘沽区| 左云县| 通渭县| 堆龙德庆县| 体育| 余庆县| 新密市| 灌南县| 那曲县| 镇远县| 辽阳市| 商城县| 云浮市| 呼图壁县|