在Windows Batch和Bash中通过单词读取文件

我需要从文件中打印每个单词的重复次数。 但我不明白如何阅读Win CMD和Bash中的文字。 我该怎么做?

仅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