如何从ubuntu中的文本文件生成(唯一)单词列表?

I Z*_*I Z 8 ubuntu words unique

我有一个ASCII文本文件.我想使用一个或多个Ubuntu命令从该文件生成所有"单词"的列表.单词定义为分隔符之间的alpha-num序列.分隔符默认是空格,但我也想尝试其他字符,如标点符号等.换句话说,我希望能够指定分隔符字符集.我如何只生成一组独特的单词?如果我还想仅列出长度至少为N个字符的单词,该怎么办?

Abd*_*aib 24

你可以使用grep:

-E'\ w +'搜索单词 - 只打印匹配%cat temp的行部分有些例子使用"快速棕色狐狸跳过懒狗",而不是"Lorem ipsum dolor sit amet,consectetur adipiscing elit "例如文字.

如果你不在乎是否重复单词

% grep -o -E '\w+' temp
Some
examples
use
The
quick
brown
fox
jumped
over
the
lazy
dog
rather
than
Lorem
ipsum
dolor
sit
amet
consectetur
adipiscing
elit
for
example
text
Run Code Online (Sandbox Code Playgroud)

如果您只想打印每个单词一次,无视大小写,您可以使用排序

-u只打印每个单词一次-f告诉sort在比较单词时忽略大小写

如果你只想要每个单词一次

% grep -o -E '\w+' temp | sort -u -f
adipiscing
amet
brown
consectetur
dog
dolor
elit
example
examples
for
fox
ipsum
jumped
lazy
Lorem
over
quick
rather
sit
Some
text
than
The
use
Run Code Online (Sandbox Code Playgroud)

你也可以使用tr命令

echo the quick brown fox jumped over the lazydog | tr -cs 'a-zA-Z0-9' '\n'
the
quick
brown
fox
jumped
over
the
lazydog
Run Code Online (Sandbox Code Playgroud)

-c是为指定字符的补; 所述-s挤出的替代的重复; 'a-zA-Z0-9'是一组字母数字,如果你在这里添加一个字符,输入将不会在该字符上分隔(参见下面的另一个例子); '\n'是替换字符(换行符).

echo the quick brown fox jumped over the lazy-dog | tr -cs 'a-zA-Z0-9-' '\n'
the
quick
brown
fox
jumped
over
the
lazy-dog
Run Code Online (Sandbox Code Playgroud)

当我们在非分隔符列表中添加" - "时,就会打印出懒狗.其他输出是

echo the quick brown fox jumped over the lazy-dog | tr -cs 'a-zA-Z0-9' '\n'
the
quick
brown
fox
jumped
over
the
lazy
dog
Run Code Online (Sandbox Code Playgroud)

tr的摘要:任何不在参数中的字符-c都将作为分隔符.我希望这也解决了你的分隔符问题.


小智 5

这是我的词云链

cat myfile | grep -o -E '\w+' | tr '[A-Z]' '[a-z]' | sort | uniq -c | sort -nr

如果您有 tex 文件,请替换catdetex

detex myfile | grep -o -E '\w+' | tr '[A-Z]' '[a-z]' | sort | uniq -c | sort -nr