-->

如何生成的(唯一的)从文本文件在Ubuntu话清单?(how to generate list of

2019-09-02 13:08发布

我有一个ASCII文本文件。 我要生成使用一个或多个Ubuntu的命令从该文件中所有的“字”的列表。 一个字被定义为分隔符之间的α-NUM序列。 分隔符默认情况下空格,但我也想与其他字符,如标点符号等等。换句话说去尝试,我希望能够指定一个分隔符字符集。 我怎么只生产了一套独特的话? 如果我也想仅列出至少N个字符长的那些话?

Answer 1:

你可以使用grep:

-E“\ w +”单词搜索-o只打印匹配%的猫温度一些例子使用“敏捷的棕色狐狸跳过了懒狗”,而不是该行的部分“Lorem存有悲坐阿梅德,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

如果你想只打印一次每个字,无论情况下,可以使用排序

-u只有一次-f告诉排序比较词时忽略的情况下打印每个字

如果你只是想一次每个字

% 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

你也可以使用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

-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

当我们加入“ - ”在非分隔符列表清单,懒惰的狗被打印出来。 其他输出

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

摘要TR:任何字符没有在参数-c ,将作为一个分隔符。 我希望这也解决了您的分隔符的问题。



Answer 2:

这应该为你工作:

tr \ \\t\\v\\f\\r \\n | | tr -s \\n | tr -dc a-zA-Z0-9\\n | LC_ALL=C sort | uniq

如果您想获得至少5个字符的文字,管的输出tr通过grep ..... 。 如果你想不区分大小写,坚持tr AZ az在之前流水线的某个地方sort

需要注意的是LC_ALL=C是必要的sort才能正常工作。

我建议你阅读的man对你不明白这里的蚂蚁命令页。



Answer 3:

这里是我的话,云想链

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



文章来源: how to generate list of (unique) words from text file in ubuntu?