简体   繁体   中英

And statement using grep/regular expressions

I'm using grep to search for text within a specific directory. I would like to return rows of text that contain stringA AND stringB.

I know that doing grep "stringA|stringB" is effectively an OR statement, is there something I can do, maybe using regex, that would allow me to run an AND statement ?

Many Thanks

If you don't know the order of the items, you could always reverse a pattern using |

grep '1st pattern.*2nd pattern|2nd pattern.*1st pattern' foofile

This works geat with two items, three or more would start slowing things down for sure...

You can pipe through two greps:

 ... | grep "stringA" | grep "stringB"

Note that if your patterns are actually fixed strings and not regular expressions then you can use fgrep instead of grep .

"stringA.*stringB"

最后将在stringB之间找到具有任意数量的任何字符的stringA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM