[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理如何统计txt文本中各个数字岀现的次数

本帖最后由 pcl_test 于 2016-11-21 19:46 编辑

文件夹中有多个txt文档,想统计毎个txt文档中各个数字岀现的次数,把毎个txt文档统计结果保存到一个新txt中。谢谢!

呵呵!附件传不了,弄了个样本;
D:\测试\nest001.txt、nest002.txt、nest003.txt......等多个txt文档,文档中假如是从1-50的整数,想统计每个文档中各个数字岀现的次数,汇总到另一个文档中,示例如下;
nest001.txt
  1. 01 04 05 11 29 30
  2. 02 09 14 19 21 30
  3. 01 11 18 20 28 29
  4. 02 04 12 18 23 31
  5. 02 03 12 13 14 25
  6. 05 08 12 13 23 25
  7. 03 05 14 18 25 33
  8. 02 14 17 27 28 31
  9. 06 09 15 24 25 26
  10. 03 08 17 21 22 31
  11. 02 09 15 16 29 32
  12. 03 04 05 08 10 22
  13. 02 05 15 17 18 21
  14. 01 06 07 17 18 23
  15. 04 12 13 22 27 29
  16. 05 12 17 19 25 30
  17. 01 07 09 19 28 29
  18. 02 04 12 17 22 25
  19. 01 03 07 13 19 32
  20. 01 12 16 20 30 33
复制代码
nest002.txt
  1. 03 09 15 20 27 29
  2. 04 21 23 31 32 33
  3. 06 10 11 28 30 33
  4. 01 04 19 22 24 25
  5. 15 18 23 27 32 33
  6. 03 04 07 17 21 27
  7. 08 10 12 14 18 28
  8. 05 14 16 21 29 30
  9. 08 09 19 20 25 32
  10. 05 07 08 20 31 33
  11. 09 10 13 14 21 32
复制代码
nest003.txt
  1. 10 12 13 23 26 29
  2. 01 06 13 20 29 32
  3. 06 07 22 26 31 32
  4. 01 02 05 12 15 16
  5. 07 09 10 15 19 33
  6. 03 08 14 22 24 32
  7. 04 05 08 11 21 27
  8. 08 13 15 20 21 25
  9. 01 07 09 16 20 23
  10. 07 15 16 25 28 32
  11. 10 15 20 23 24 31
  12. 02 14 15 16 23 24
  13. 07 10 16 17 18 32
复制代码
统计结果.txt
  1. 需求说明;
  2.   在样本测试文件夹中的所有txt中,有从1-50的自然整数(不超过100),想得到以下统计结果,
  3. 现在的结果是我手工统计的,如有误请谅解,谢谢!
  4. D:\测试\nest001.txt
  5. 00次:34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  6. 01次:10 24 26  
  7. 02次:06 11 16 20 27 32 33  
  8. 03次:07 08 15 21 23 28 31  
  9. 04次:09 13 14 19 22 30  
  10. 05次:03 04 18 29  
  11. 06次:01 05 17 25  
  12. 07次:02 12  
  13. D:\测试\nest002.txt
  14. 00次:02 26 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  15. 01次:01 06 11 12 13 16 17 22 24  
  16. 02次:03 05 07 15 18 19 23 25 28 29 30 31  
  17. 03次:04 08 09 10 14 20 27  
  18. 04次:21 32 33  
  19. D:\测试\nest003.txt
  20. 00次:30 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  21. 01次:03 04 11 17 18 19 27 28 33  
  22. 02次:02 05 06 09 12 14 21 22 25 26 29 31  
  23. 03次:01 08 13 24  
  24. 04次:10 20 23  
  25. 05次:07 16 32  
  26. 06次:15  
复制代码

本帖最后由 pcl_test 于 2016-11-21 22:34 编辑
  1. rem win7及以上系统运行
  2. powershell -c "$max=50;dir *.txt|%%{$_.FullName;$h=@{};$a=([IO.File]::ReadAllText($_, [Text.Encoding]::Default).trim() -split '\s+'|group|group Count|sort Name|%%{$_.Name+':'+(($_.Group|%%{$h[$_.Name]=1;$_.Name}|sort) -join ' ')});$b=@();1..$max|%%{$t='{0:d2}' -f $_;if(!$h[$t]){$b+=(,$t)}};if($b.Count -eq 0){$a}else{'0:'+(($b|sort) -join ' ');$a};''}|out-file '统计结果.log' -encoding Default"
  3. pause
复制代码

TOP

佩服,批处理写得出神入化。

TOP

  1. @echo off
  2. for /l %%i in (100 1 199) do set "_%%i=0"
  3. (for %%i in (*.txt) do (
  4.         echo,%%i
  5.         setlocal enabledelayedexpansion
  6.         for /f "usebackq delims=" %%a in ("%%i") do (
  7.                 for %%b in (%%a) do set /a _1%%b+=1
  8.         )
  9.         (for /l %%a in (100 1 199) do (
  10.                 set "a=00!_%%a!"
  11.                 echo,!a:~-2! %%a
  12.         ))>$
  13.         for /f "tokens=1*" %%a in ('sort $ ^& echo,/') do (
  14.                 if "%%a" NEQ "!Count!" (
  15.                        if defined Count echo,!Count!次:!s!
  16.                        set "s="
  17.                 )
  18.                 set "Count=%%a"
  19.                 set "Num=%%b"
  20.                 set "s=!s! !Num:~1!"
  21.         )
  22.         endlocal
  23. ))>结果.Log
  24. del $
  25. pause
复制代码

TOP

回复 5# yiwuyun


    辛苦了

TOP

  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. set txt=1.txt
  4. set __yiwuyun.txt=___yiwuyun.txt
  5. set "space= "
  6. set /a Number=50
  7. set /a maxNumber=!Number!-1
  8. set /a suzu.MaxRow=0
  9. for /l %%a in (0,1,!maxNumber!) do (
  10.   if %%a lss 9 (
  11.     set /a tempb=%%a+1
  12.     set suzu0.%%a=0!tempb!
  13.   ) else (
  14.     set /a suzu0.%%a=%%a+1
  15.   )
  16. )
  17. set /a suzu0.MaxCol=!maxNumber!
  18. for /f "delims=" %%a in ('type !txt!') do (
  19.   set "string=%%a"
  20.   set "s1="
  21.   call :GetTokenTextNAndCount "^!string^!" "^!space^!"
  22.   for /l %%c in (1,1,!tokenTextCount!) do (
  23.     call :GetRowColInSuzu !suzu.MaxRow! !tokenText%%c!
  24.     call :DeleteFromSuzu !rowInSuzu! !colInSuzu!
  25.     if !rowInSuzu! equ !suzu.MaxRow! (
  26.       set /a rowInSuzu+=1
  27.       set /a suzu.MaxRow=!rowInSuzu!
  28.       set suzu!suzu.MaxRow!.0=!tokenText%%c!
  29.       set /a suzu!suzu.MaxRow!.MaxCol=0
  30.     ) else (
  31.       set /a rowInSuzu+=1
  32.       call :AddToSuzu !rowInSuzu! !tokenText%%c!
  33.     )
  34.   )
  35. )
  36. call :PrintSuzu
  37. pause
  38. exit /b 0
  39. :PrintSuzu
  40. for /l %%a in (0,1,!suzu.MaxRow!) do (
  41.   for /l %%b in (0,1,!suzu%%a.MaxCol!) do (
  42.     if %%b equ !suzu%%a.MaxCol! (
  43.       set "s=!s!!suzu%%a.%%b!"
  44.     ) else (
  45.       set "s=!s!!suzu%%a.%%b! "
  46.     )
  47.   )
  48.   echo.%%a: !s!
  49.   set "s="
  50. )
  51. exit /b 0
  52. :GetRowColInSuzu
  53. for /l %%a in (0,1,%1) do (
  54.   for /l %%b in (0,1,!suzu%%a.MaxCol!) do (
  55.     if "!suzu%%a.%%b!"=="%2" (
  56.       set /a rowInSuzu=%%a
  57.       set /a colInSuzu=%%b
  58.       goto endGetRowColInSuzu
  59.     )
  60.   )
  61. )
  62. :endGetRowColInSuzu
  63. exit /b 0
  64. :DeleteFromSuzu
  65. if %2 equ !suzu%1.MaxCol! (
  66.   set /a suzu%1.MaxCol-=1
  67. ) else (
  68.   set /a nDeleteFromSuzu=!suzu%1.MaxCol!-1
  69.   for /l %%a in (%2,1,!nDeleteFromSuzu!) do (
  70.     set /a n=%%a+1
  71.     for /l %%b in (!n!,1,!n!) do set suzu%1.%%a=!suzu%1.%%b!
  72.   )
  73.   set /a suzu%1.MaxCol=!nDeleteFromSuzu!
  74. )
  75. exit /b 0
  76. :AddToSuzu
  77. set /a max=!suzu%1.MaxCol!
  78. if !max! equ -1 (
  79.   set suzu%1.MaxCol=0
  80.   set "suzu%1.0=%2"
  81. ) else (
  82.   for /l %%a in (0,1,!max!) do (
  83.     set /a p=%%a+1
  84.     if "%2" lss "!suzu%1.%%a!" (
  85.       set /a m=!suzu%1.MaxCol!+1
  86.       for /l %%b in (!m!,-1,!p!) do (
  87.         set /a k=%%b-1
  88.         for /l %%c in (!k!,1,!k!) do set suzu%1.%%b=!suzu%1.%%c!
  89.       )
  90.       set suzu%1.%%a=%2
  91.       set /a suzu%1.MaxCol=!m!
  92.       goto end
  93.     ) else (
  94.       if "%%a"=="!max!" (
  95.         set /a suzu%1.MaxCol+=1
  96.         set suzu%1.!suzu%1.MaxCol!=%2
  97.       )
  98.     )
  99.   )
  100. )
  101. :end
  102. exit /b 0
  103. :GetStringLength
  104. setlocal
  105. set string=!string!
  106. set /a n=0
  107. :startGetStringLength
  108. if not "!string!"=="" (
  109.   set string=!string:~1!
  110.   set /a n=!n!+1
  111.   goto startGetStringLength
  112. )
  113. set /a stringLength=!n!
  114. endlocal&set stringLength=%stringLength%
  115. exit /b 0
  116. :GetTokenPlaceNAndCount
  117. setlocal
  118. set "string=%~1"
  119. set "secondParameter=%~2"
  120. call :GetStringLength
  121. set /a nGetTokenPlaceNAndCount=!stringLength!-1
  122. set /a nToken=1
  123. set /a tokenPlace0=-1
  124. for /l %%a in (0,1,!nGetTokenPlaceNAndCount!) do (
  125.   if "!string:~%%a,1!"=="!secondParameter!" (
  126.      set /a tokenPlace!nToken!=%%a
  127.      set /a nToken+=1
  128.   )
  129. )
  130. set /a tokenPlace!nToken!=!stringLength!
  131. set /a tokenCount=!nToken!-1
  132. type nul>!__yiwuyun.txt!
  133. for /l %%a in (0,1,!nToken!) do (
  134.    echo set /a tokenPlace%%a=!tokenPlace%%a!>>!__yiwuyun.txt!
  135. )
  136. echo set /a tokenCount=!tokenCount!>>!__yiwuyun.txt!
  137. endlocal
  138. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  139.   %%a
  140. )
  141. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  142. exit /b 0
  143. :GetTokenTextNAndCount
  144. set "string=%~1"
  145. set "___secondParameter=%~2"
  146. call :GetTokenPlaceNAndCount "^!string^!" "^!___secondParameter^!"
  147. set /a ___next=0
  148. set /a tokenTextCount=!tokenCount!+1
  149. set /a ___n=0
  150. :startGetTokenTextNAndCount
  151.   set /a ___beforePlace=!tokenPlace%___next%!
  152.   set /a ___first=!___beforePlace!+1
  153.   set /a ___next=!___next!+1
  154.   set /a ___afterPlace=!tokenPlace%___next%!
  155.   set /a ___second=!___afterPlace!-!___beforePlace!-1
  156.   set /a ___n=!___n!+1
  157.   if "!string!"=="" (
  158.     set "tokenText!___n!="
  159.   ) else (
  160.     set tokenText!___n!=!string:~%___first%,%___second%!
  161.   )
  162. if !___n! lss !tokenTextCount! goto startGetTokenTextNAndCount
  163. exit /b 0
复制代码
1

评分人数

TOP

  1. @set @n=0;/* & echo off
  2. (for %%a in (*.txt) do (
  3.         echo,%%a
  4.         cscript -nologo -e:jscript "%~0"<"%%a"
  5. ))>$
  6. move $ 统计结果.txt
  7. pause & exit/b & rem */
  8. txt = WScript.StdIn.ReadAll();
  9. arr = [];
  10. for (i=100; i<=199; i++) {
  11.         s1 = (i + "").substr(1);
  12.         len = txt.split(s1).length - 1;
  13.         arr.push(len + " " + s1)
  14. }
  15. arr.sort(function(x,y){return x.split(" ")[0]-y.split(" ")[0]});
  16. arr.push("");
  17. s = "";
  18. for (i=0; i<arr.length-1; i++) {
  19.         a = arr[i].split(" ");
  20.         s += " " + a[1];
  21.         if (arr[i+1].split(" ")[0] != a[0]) {
  22.                 WScript.Echo(a[0] + "次:" + s);
  23.                 s = "";
  24.         }
  25. }
复制代码

TOP

数字是指0-9 还是固有的

TOP

请给出样本

TOP

返回列表