你可以使用 grep:
-E '\w+' 搜索字词
-o 仅打印与% 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
如果您只想打印每个单词一次,不考虑大小写,您可以使用排序
-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
您也可以使用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 参数中的字符都将充当分隔符。我希望这也能解决您的分隔符问题。