regex - Linux 命令 - 查找长度为 3 个或 8 个字母的单词

标签 regex grep

我有一个包含很多单词的文本文件。我需要找到长度为 3 个字母或 8 个字母的单词。我可以使用以下命令分别找到 3 和 8 个字母的单词。如何将结果合并到一个输出中?

grep -E '^.{3}$' words | wc -w

grep -E '^.{8}$' words | wc -w

最佳答案

另一种方式是:

grep -E '^(.{3}|.{8})$'

将其他方式放在一起:

grep -E '^.{3}$|^.{8}$'
grep -E -e '^.{3}$' -e '^.{8}$'

检查这个:Alternation with The Vertical Bar or Pipe Symbol

一个例子:

$ cat file
orange
banana
who
what
we
eat
buzzkill
find

$ grep -E '^(.{3}|.{8})$' file
who
eat
buzzkill

$ grep -E '^.{3}$|^.{8}$' file
who
eat
buzzkill

$ grep -E -e '^.{3}$' -e '^.{8}$' file
who
eat
buzzkill

关于regex - Linux 命令 - 查找长度为 3 个或 8 个字母的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54856632/

相关文章:

javascript - 正则表达式至少 10 个字符,包含 1 个数字和 1 个大写字母

php - 如何删除前导和尾随非字母数字字符

正则表达式匹配所有单词对

sed - grep : Find occurances of 3 consecutive alphabets

javascript - 如何使用 ES6/7 对 html 元素中的所有文本运行大量正则表达式替换?

python - 查找一个系列在另一个系列中的匹配项,并使用正则表达式匹配打印整行数据帧

java - 如何将正则表达式匹配推送到java中的数组?

linux - Grep 包含指定字符串的文件列表并获取包含文件名和所需数据的输出

linux - 是否有任何等效的命令 grep -nP "\t"some_file ,使用 sed 或 awk

linux - Grep 组行