问题描述
假设我有几个字符串:str1、str2 和 str3.
Suppose I have several strings: str1 and str2 and str3.
- 如何查找包含所有字符串的行?
- 如何查找可以包含其中任何一行的行?
- 以及如何查找具有 str1 和 str2 和 str3 之一的行[但不是两者?]?
推荐答案
这看起来像是三个问题.将这些类型的表达式放在一起的最简单方法是使用多个管道.没有什么可耻的,特别是因为正则表达式(使用 egrep)会很笨拙,因为你看起来暗示您想要订单独立性.
This looks like three questions. The easiest way to put these sorts of expressions together is with multiple pipes. There's no shame in that, particularly because a regular expression (using egrep) would be ungainly since you seem to imply you want order independence.
所以,按顺序,
grep str1 |grep str2 |grep str3
grep str1 | grep str2 | grep str3
egrep '(str1|str2|str3)'
egrep '(str1|str2|str3)'
grep str1 |egrep '(str2|str3)'
grep str1 | egrep '(str2|str3)'
您可以使用 egrep 以独立于顺序的方式执行and"形式,但我认为您会发现使用管道 grep 执行独立于顺序的 ands 和使用正则表达式执行独立于顺序的 or 会更容易记住.
you can do the "and" form in an order independent way using egrep, but I think you'll find it easier to remember to do order independent ands using piped greps and order independent or's using regular expressions.
这篇关于grep egrep 多字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!