CODE: [Copy to clipboard]
闲来无聊,瞎搞一个,测试
功能:统计每个单词在文本中出现的次数
用法:
cmd下
word_num 文件名
统计结果样式:
0152 fixed
0108 added
0094 in
0091 to
0070 will
0069 not
0062 problem
0059 the
0056 bar
0054 support
0054 at
0053 and
0050 for
0047 menu
0046 tab
0042 when
0041 new
0040 option
0040 changed
0038 myie2
0037 plugin
0036 favorite
0035 toolbar
0035 on
代码
@echo off
cls
echo.
echo.-----------------------------
echo....统计中.....请少等...
echo.-----------------------------
echo.
if "%*" "" goto :syntax
if "%*" "/?" goto :syntax
if "%*" "/" goto :syntax
if "%*" "?" goto :syntax
if /i "%*" "/help" goto :syntax
:: /* 拆解文本为每行一单词 */
setlocal ENABLEDELAYEDEXPANSION
del end.txt tmp*.txt 2>nul
set /a m=0
:loop
set /a m+=1
for /f "tokens=%m% delims=.,:;/[]()!'@=\<> " %%i in (%*) do (
set n=%%i
set n=!n:"=!
echo.1 !n!>>tmp.txt
)
if "%m%" "200" goto:add
goto:loop
endlocal
:add
:: /* 分类汇总 */
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1,2" %%i in ('sort tmp.txt^|findstr /r "^1\ [a-z][A-
Z]"') do (
if /i "!y!" "%%j" (
set /a x+=1) else (if "!x!" neq "" (
if !x! lss 10 (echo.000!x! !y!>>tmp1.txt) else (
if !x! lss 100 (echo.00!x! !y!>>tmp1.txt) else (
if !x! lss 1000 (echo.0!x! !y!>>tmp1.txt))))
set y=%%j
set x=%%i)
)
if !x! lss 10 (echo.000!x! !y!>>tmp1.txt) else (
if !x! lss 100 (echo.00!x! !y!>>tmp1.txt) else (
if !x! lss 1000 (echo.0!x! !y!>>tmp1.txt)))
endlocal
sort /r tmp1.txt>tmp2.txt
:ex
:: /* 大写转小写 */
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (tmp2.txt) do (
set ex=%%i
set ex=!ex:A=a!
set ex=!ex:B=b!
set ex=!ex:C=c!
set ex=!ex:D=d!
set ex=!ex:E=e!
set ex=!ex:F=f!
set ex=!ex:G=g!
set ex=!ex:H=h!
set ex=!ex:I=i!
set ex=!ex:J=j!
set ex=!ex:K=k!
set ex=!ex:L=l!
set ex=!ex:M=m!
set ex=!ex:N=n!
set ex=!ex:O=o!
set ex=!ex:P=p!
set ex=!ex:Q=q!
set ex=!ex:R=r!
set ex=!ex:S=s!
set ex=!ex:T=t!
set ex=!ex:U=u!
set ex=!ex:V=v!
set ex=!ex:W=w!
set ex=!ex:X=x!
set ex=!ex:Y=y!
set ex=!ex:Z=z!
echo.!ex!>>end.txt
)
del tmp*.txt
start end.txt
:syntax
cls
echo.
echo.word_num.cmd
echo.
echo.written by hitme 2005.9.2
echo.用法:
echo.word_num 文件名
echo.
echo.例如:
echo.word_num License.txt
echo.
统计中文字的,用法同顶楼,想
办法搞成竖排一列,试试
代码:
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (%1) do (
set n=%%i
echo.!n!>tmp.txt
echo.x>>tmp.txt
for /f "tokens=1 delims=:" %%a in ('findstr /o "x" tmp.txt') do set lenth=%%a
for /l %%a in (0,1,!lenth!) do (
set m=!n:~%%a,1!
if "!m!" neq "" echo.1 !m!>>tmp1.txt
)
)
:add
for /f "tokens=1,2" %%i in ('sort tmp1.txt') do (
if "!y!" "%%j" (
set /a x+=1) else ( if "!x!" neq "" (
if !x! lss 10 (echo.000!x!
!y!>>tmp2.txt) else (
if !x! lss 100 (echo.00!x!
!y!>>tmp2.txt) else (
if !x! lss 1000 (echo.0!x!
!y!>>tmp2.txt))))
set y=%%j
set x=%%i)
)
if !x! lss 10 (echo.000!x! !y!>>tmp2.txt) else (
if !x! lss 100 (echo.00!x! !y!>>tmp2.txt) else (
if !x! lss 1000 (echo.0!x! !y!>>tmp2.txt)))
endlocal
sort /r tmp2.txt>end.txt
del tmp*.txt
start end.txt