标题: [文本处理] 批处理如何统计txt文本中各个数字岀现的次数 [打印本页]
作者: hkldd 时间: 2015-1-15 11:20 标题: 批处理如何统计txt文本中各个数字岀现的次数
本帖最后由 pcl_test 于 2016-11-21 19:46 编辑
文件夹中有多个txt文档,想统计毎个txt文档中各个数字岀现的次数,把毎个txt文档统计结果保存到一个新txt中。谢谢!
呵呵!附件传不了,弄了个样本;
D:\测试\nest001.txt、nest002.txt、nest003.txt......等多个txt文档,文档中假如是从1-50的整数,想统计每个文档中各个数字岀现的次数,汇总到另一个文档中,示例如下;
nest001.txt-
- 01 04 05 11 29 30
- 02 09 14 19 21 30
- 01 11 18 20 28 29
- 02 04 12 18 23 31
- 02 03 12 13 14 25
- 05 08 12 13 23 25
- 03 05 14 18 25 33
- 02 14 17 27 28 31
- 06 09 15 24 25 26
- 03 08 17 21 22 31
- 02 09 15 16 29 32
- 03 04 05 08 10 22
- 02 05 15 17 18 21
- 01 06 07 17 18 23
- 04 12 13 22 27 29
- 05 12 17 19 25 30
- 01 07 09 19 28 29
- 02 04 12 17 22 25
- 01 03 07 13 19 32
- 01 12 16 20 30 33
复制代码
nest002.txt-
- 03 09 15 20 27 29
- 04 21 23 31 32 33
- 06 10 11 28 30 33
- 01 04 19 22 24 25
- 15 18 23 27 32 33
- 03 04 07 17 21 27
- 08 10 12 14 18 28
- 05 14 16 21 29 30
- 08 09 19 20 25 32
- 05 07 08 20 31 33
- 09 10 13 14 21 32
复制代码
nest003.txt-
- 10 12 13 23 26 29
- 01 06 13 20 29 32
- 06 07 22 26 31 32
- 01 02 05 12 15 16
- 07 09 10 15 19 33
- 03 08 14 22 24 32
- 04 05 08 11 21 27
- 08 13 15 20 21 25
- 01 07 09 16 20 23
- 07 15 16 25 28 32
- 10 15 20 23 24 31
- 02 14 15 16 23 24
- 07 10 16 17 18 32
复制代码
统计结果.txt-
- 需求说明;
- 在样本测试文件夹中的所有txt中,有从1-50的自然整数(不超过100),想得到以下统计结果,
- 现在的结果是我手工统计的,如有误请谅解,谢谢!
-
- D:\测试\nest001.txt
- 00次:34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
- 01次:10 24 26
- 02次:06 11 16 20 27 32 33
- 03次:07 08 15 21 23 28 31
- 04次:09 13 14 19 22 30
- 05次:03 04 18 29
- 06次:01 05 17 25
- 07次:02 12
- D:\测试\nest002.txt
- 00次:02 26 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
- 01次:01 06 11 12 13 16 17 22 24
- 02次:03 05 07 15 18 19 23 25 28 29 30 31
- 03次:04 08 09 10 14 20 27
- 04次:21 32 33
- D:\测试\nest003.txt
- 00次:30 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
- 01次:03 04 11 17 18 19 27 28 33
- 02次:02 05 06 09 12 14 21 22 25 26 29 31
- 03次:01 08 13 24
- 04次:10 20 23
- 05次:07 16 32
- 06次:15
复制代码
作者: CrLf 时间: 2015-1-15 20:46
请给出样本
作者: terse 时间: 2015-1-15 22:56
数字是指0-9 还是固有的
作者: apang 时间: 2015-1-16 15:01
- @set @n=0;/* & echo off
- (for %%a in (*.txt) do (
- echo,%%a
- cscript -nologo -e:jscript "%~0"<"%%a"
- ))>$
- move $ 统计结果.txt
- pause & exit/b & rem */
-
- txt = WScript.StdIn.ReadAll();
- arr = [];
- for (i=100; i<=199; i++) {
- s1 = (i + "").substr(1);
- len = txt.split(s1).length - 1;
- arr.push(len + " " + s1)
- }
- arr.sort(function(x,y){return x.split(" ")[0]-y.split(" ")[0]});
- arr.push("");
- s = "";
- for (i=0; i<arr.length-1; i++) {
- a = arr[i].split(" ");
- s += " " + a[1];
- if (arr[i+1].split(" ")[0] != a[0]) {
- WScript.Echo(a[0] + "次:" + s);
- s = "";
- }
- }
复制代码
作者: yiwuyun 时间: 2015-1-17 18:51
- @echo off
- setlocal EnableDelayedExpansion
- set txt=1.txt
- set __yiwuyun.txt=___yiwuyun.txt
- set "space= "
- set /a Number=50
- set /a maxNumber=!Number!-1
- set /a suzu.MaxRow=0
- for /l %%a in (0,1,!maxNumber!) do (
- if %%a lss 9 (
- set /a tempb=%%a+1
- set suzu0.%%a=0!tempb!
- ) else (
- set /a suzu0.%%a=%%a+1
- )
- )
- set /a suzu0.MaxCol=!maxNumber!
- for /f "delims=" %%a in ('type !txt!') do (
- set "string=%%a"
- set "s1="
- call :GetTokenTextNAndCount "^!string^!" "^!space^!"
- for /l %%c in (1,1,!tokenTextCount!) do (
- call :GetRowColInSuzu !suzu.MaxRow! !tokenText%%c!
- call :DeleteFromSuzu !rowInSuzu! !colInSuzu!
- if !rowInSuzu! equ !suzu.MaxRow! (
- set /a rowInSuzu+=1
- set /a suzu.MaxRow=!rowInSuzu!
- set suzu!suzu.MaxRow!.0=!tokenText%%c!
- set /a suzu!suzu.MaxRow!.MaxCol=0
- ) else (
- set /a rowInSuzu+=1
- call :AddToSuzu !rowInSuzu! !tokenText%%c!
- )
- )
- )
- call :PrintSuzu
- pause
- exit /b 0
-
-
- :PrintSuzu
- for /l %%a in (0,1,!suzu.MaxRow!) do (
- for /l %%b in (0,1,!suzu%%a.MaxCol!) do (
- if %%b equ !suzu%%a.MaxCol! (
- set "s=!s!!suzu%%a.%%b!"
- ) else (
- set "s=!s!!suzu%%a.%%b! "
- )
- )
- echo.%%a: !s!
- set "s="
- )
- exit /b 0
-
-
- :GetRowColInSuzu
- for /l %%a in (0,1,%1) do (
- for /l %%b in (0,1,!suzu%%a.MaxCol!) do (
- if "!suzu%%a.%%b!"=="%2" (
- set /a rowInSuzu=%%a
- set /a colInSuzu=%%b
- goto endGetRowColInSuzu
- )
- )
- )
- :endGetRowColInSuzu
- exit /b 0
-
-
- :DeleteFromSuzu
- if %2 equ !suzu%1.MaxCol! (
- set /a suzu%1.MaxCol-=1
- ) else (
- set /a nDeleteFromSuzu=!suzu%1.MaxCol!-1
- for /l %%a in (%2,1,!nDeleteFromSuzu!) do (
- set /a n=%%a+1
- for /l %%b in (!n!,1,!n!) do set suzu%1.%%a=!suzu%1.%%b!
- )
- set /a suzu%1.MaxCol=!nDeleteFromSuzu!
- )
- exit /b 0
-
-
- :AddToSuzu
- set /a max=!suzu%1.MaxCol!
- if !max! equ -1 (
- set suzu%1.MaxCol=0
- set "suzu%1.0=%2"
- ) else (
- for /l %%a in (0,1,!max!) do (
- set /a p=%%a+1
- if "%2" lss "!suzu%1.%%a!" (
- set /a m=!suzu%1.MaxCol!+1
- for /l %%b in (!m!,-1,!p!) do (
- set /a k=%%b-1
- for /l %%c in (!k!,1,!k!) do set suzu%1.%%b=!suzu%1.%%c!
- )
- set suzu%1.%%a=%2
- set /a suzu%1.MaxCol=!m!
- goto end
- ) else (
- if "%%a"=="!max!" (
- set /a suzu%1.MaxCol+=1
- set suzu%1.!suzu%1.MaxCol!=%2
- )
- )
- )
- )
- :end
- exit /b 0
-
-
-
- :GetStringLength
- setlocal
- set string=!string!
- set /a n=0
- :startGetStringLength
- if not "!string!"=="" (
- set string=!string:~1!
- set /a n=!n!+1
- goto startGetStringLength
- )
- set /a stringLength=!n!
- endlocal&set stringLength=%stringLength%
- exit /b 0
-
-
- :GetTokenPlaceNAndCount
- setlocal
- set "string=%~1"
- set "secondParameter=%~2"
- call :GetStringLength
- set /a nGetTokenPlaceNAndCount=!stringLength!-1
- set /a nToken=1
- set /a tokenPlace0=-1
- for /l %%a in (0,1,!nGetTokenPlaceNAndCount!) do (
- if "!string:~%%a,1!"=="!secondParameter!" (
- set /a tokenPlace!nToken!=%%a
- set /a nToken+=1
- )
- )
- set /a tokenPlace!nToken!=!stringLength!
- set /a tokenCount=!nToken!-1
- type nul>!__yiwuyun.txt!
- for /l %%a in (0,1,!nToken!) do (
- echo set /a tokenPlace%%a=!tokenPlace%%a!>>!__yiwuyun.txt!
- )
- echo set /a tokenCount=!tokenCount!>>!__yiwuyun.txt!
- endlocal
- for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
- %%a
- )
- if exist !__yiwuyun.txt! del !__yiwuyun.txt!
- exit /b 0
-
-
- :GetTokenTextNAndCount
- set "string=%~1"
- set "___secondParameter=%~2"
- call :GetTokenPlaceNAndCount "^!string^!" "^!___secondParameter^!"
- set /a ___next=0
- set /a tokenTextCount=!tokenCount!+1
- set /a ___n=0
- :startGetTokenTextNAndCount
- set /a ___beforePlace=!tokenPlace%___next%!
- set /a ___first=!___beforePlace!+1
- set /a ___next=!___next!+1
- set /a ___afterPlace=!tokenPlace%___next%!
- set /a ___second=!___afterPlace!-!___beforePlace!-1
- set /a ___n=!___n!+1
- if "!string!"=="" (
- set "tokenText!___n!="
- ) else (
- set tokenText!___n!=!string:~%___first%,%___second%!
- )
- if !___n! lss !tokenTextCount! goto startGetTokenTextNAndCount
- exit /b 0
复制代码
作者: apang 时间: 2015-1-17 20:14
回复 5# yiwuyun
辛苦了
作者: apang 时间: 2015-1-17 20:17
- @echo off
- for /l %%i in (100 1 199) do set "_%%i=0"
- (for %%i in (*.txt) do (
- echo,%%i
- setlocal enabledelayedexpansion
- for /f "usebackq delims=" %%a in ("%%i") do (
- for %%b in (%%a) do set /a _1%%b+=1
- )
- (for /l %%a in (100 1 199) do (
- set "a=00!_%%a!"
- echo,!a:~-2! %%a
- ))>$
- for /f "tokens=1*" %%a in ('sort $ ^& echo,/') do (
- if "%%a" NEQ "!Count!" (
- if defined Count echo,!Count!次:!s!
- set "s="
- )
- set "Count=%%a"
- set "Num=%%b"
- set "s=!s! !Num:~1!"
- )
- endlocal
- ))>结果.Log
- del $
- pause
复制代码
作者: yiwuyun 时间: 2015-1-17 21:01
佩服,批处理写得出神入化。
作者: pcl_test 时间: 2016-11-21 22:29
本帖最后由 pcl_test 于 2016-11-21 22:34 编辑
- rem win7及以上系统运行
- 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"
- pause
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |