我的一个作业问题是使用 grep 搜索包含单词“死亡”或“呼吸”的行。
我知道 [] 运算符充当或,但我如何使用它来指定 'd' 或 'br' ?
这不起作用:
egrep [dbr]eath test_file
(d|br)
是任何一个d or br
。
方括号用于匹配方括号中的单个字符。
例如:[asdf]ello
将匹配 aello、sello、dello 或 fello。
“|” 运算符(竖线)充当“或”。然而:
grep 'breath|death' test_file
你写的内容会找到“死亡”、“死亡”和“死亡”。
egrep '(d|br)eath' test_file
研究“交替”,你会弄明白的。
egrep 仅供参考,已弃用。采用grep -E
grep -E '(d|br)eath' file