标题: [文本处理] 【已解决】BAT如何逐列统计数据出现次数多少,并从多到少排序? [打印本页]
作者: 思想之翼 时间: 2013-12-7 00:37 标题: 【已解决】BAT如何逐列统计数据出现次数多少,并从多到少排序?
有378个txt文本,每个txt数据只有一行,为0-9十个数值的乱序排列,格式为:8 1 9 2 0 6 5 4 7 3 (约定从左到右分别为第1、2、3...10列)
欲:
统计378个txt文本中第1列的数据出现次数多少,并从多到少排序(出现次数相同的,则从小到大排序;出现次数为零的数值,也从小到大排序)
统计378个txt文本中第2列的数据出现次数多少,并从多到少排序(出现次数相同的,则从小到大排序;出现次数为零的数值,也从小到大排序)
...
统计378个txt文本中第10列的数据出现次数多少,并从多到少排序(出现次数相同的,则从小到大排序;出现次数为零的数值,也从小到大排序)
之后,将上述第1列、第2列...第10列的统计排序,合并成一行,存放在新建的txt文本中,格式为:7 5 8 0 6 3 9 1 4 2 2 1 4 5 3 7 8 9 0 6 ...
恳望得到帮助!
作者: terse 时间: 2013-12-7 17:20
- @echo off&setlocal enabledelayedexpansion
- for /L %%i in (0,1,9) do for /L %%j in (0,1,9) do set _%%i_%%j=0
- for /f "delims=" %%i in ('dir /b/a-d *.txt') do (
- for /f "usebackq delims=" %%j in ("%%i") do (
- set n=-1
- for %%a in (%%j) do (
- set /a n+=1
- set /a $!n!_%%a+=1
- )
- )
- )
- for /L %%i in (0,1,9) do for /L %%j in (0,1,9) do (
- set "str=000!$%%i_%%j!"
- set /a n=9-%%j
- set "$%%i=!$%%i! !str:~-3!#!n!-%%j"
- )
- set "str="
- for /L %%i in (0,1,9) do (
- for /f "tokens=2 delims=-" %%a in ('"(for %%j in (!$%%i!) do @echo %%j)|sort/r"') do (
- set "str=!str!%%a"
- )
- )
- >new_text.txt echo;!str!
- pause
复制代码
作者: xxpinqz 时间: 2013-12-7 17:53
- @echo off&setlocal enabledelayedexpansion
- for /l %%a in (1,1,378) do (
- set /p str=<%%a.txt
- set n=0
- for %%b in (!str!) do set /a #!n!.%%b+=1,n+=1
- )
- for /l %%a in (0,1,9) do (
- for /l %%b in (0,1,9) do (
- set /a m=0
- for /l %%c in (0,1,9) do (
- if !#%%a.%%b!%%c lss !#%%a.%%c!%%b set /a m+=1
- )
- set "!m!=%%b"
- )
- for /l %%i in (0,1,9) do set "echo=!echo!!%%i! "
- )
- echo,%echo%>new.txt
复制代码
作者: 思想之翼 时间: 2013-12-7 18:12
感谢2位大师的鼎力帮助!
作者: apang 时间: 2013-12-8 15:28
本帖最后由 apang 于 2013-12-8 15:38 编辑
纯练习- @set @n=1 //&dir /a-d/b *.txt|cscript -nologo -e:jscript "%~0" &pause&exit /b
- fso = new ActiveXObject("Scripting.FileSystemObject");
- a = new Array();b = new Array();s = new Array();
- for (i=0;i<10;i++) {a[i] = ""};
- while (!WSH.StdIn.AtEndOfStream) {
- f = WSH.StdIn.ReadLine();
- arr = fso.OpenTextFile(f).ReadLine().split(" ");
- for (i=0;i<10;i++) {a[i] += arr[i] + " "};
- }
- for (i=0;i<10;i++) {
- for (j=0;j<10;j++) {
- b[j] = a[i].split(j).length + 1000 + (9-j).toString();
- };
- b.sort(function(x,y){return y-x});
- s[i] = b.join(" ").replace(/\d{4}(\d)/g,
- function(s0,s1){return 9-s1}
- );
- };
- fso.OpenTextFile("Result.txt",2,true).Write(s.join(" "));
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |