1

我需要打印文件中每个单词的重复次数的所有单词。但我无法理解如何在 Win CMD 和 Bash 中逐字读取文件。我该怎么做?

4

1 回答 1

1

仅限 Windows 批处理

这将枚举文件中的每个单词并显示该单词及其计数。

限制

  1. 批处理行长度限制为8191 (Windows XP+)。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 回答