1

ファイルから各単語の繰り返し回数を含むすべての単語を出力する必要があります。しかし、Win CMD と Bash でファイルを単語単位で読み取る方法がわかりません。どうすればいいですか?

4

1 に答える 1

1

Windows バッチのみ

これにより、ファイル内のすべての単語が列挙され、単語とその数が表示されます。

制限事項

  1. 8191のバッチ行の長さ制限(Windows XP+)。古い OS の場合は2047 。

脚本

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /f "delims=" %%A in (file.txt) do (
    set "_=%%A"
    call :Expand_
)

:: Display the Word and Count
rem set word:
for /f "tokens=2,3 delims=:=" %%X in ('set word:') do echo %%X = %%Y
goto End


:Expand_
:: Clean Special Characters
set "_=%_:"=%"
set "_=%_:^=%"
set "_=%_:<=%"
set "_=%_:>=%"
set "_=%_:&=%"
set "_=%_:|=%"
:: Replace Whitespace
set "_=%_:  =%"
:: Remove Plurals
rem set "_=%_:'s=%"
:: Clean Punctuation
:WordLoop
for /f "tokens=1,* delims=`~!@#$%%*()-_+=\[]{};:/?., " %%X in ("%_%") do (
    set ".=%%X"
    call :Expand.
    set "_=%%Y"
)
if defined _ goto WordLoop
goto :eof

:Expand.
:: Count the Words
if defined word:%.% (
    set /a "word:%.%+=1"
) else (
    set "word:%.%=1"
)
goto :eof


:End
endlocal
于 2013-01-22T20:10:22.480 に答える