简体   繁体   English

同时使用两个正则表达式

[英]greping two regex at the same time

How to use grep to search for two regex at the same time. 如何使用grep同时搜索两个正则表达式。 Say, I am looking for "My name is" and "my bank account " in a text like: 说,我正在像这样的文本中查找“我的名字是”和“我的银行帐户”:

My name is Mike. I'm 16 years old.
I have no clue how to solve my grep problem,but
if I manage to solve it, then I'll transfer 
you some money from my bank account. 

I'd like grep to return: 我希望grep返回:

My name is my bank account 我的名字是我的银行帐户

Is it possible to do it with just one grep call or should I write a script to do that for me? 是否可以仅通过一个grep调用来完成此操作,还是应该编写一个脚本来为我执行此操作?

pipe. 管。 grep expr1 file | grep expr2

for or - egrep '(expr1|expr2)' file 用于egrep '(expr1|expr2)' file

I'm not quite sure what you're after. 我不太确定你要干什么。 The result you give doesn't seem to fit with anything grep can/will do. 您给出的结果似乎不适合grep可以/不会做的任何事情。 In particular, grep is line oriented, so if it finds a match in a line, it includes that entire line in the output. 特别地,grep是面向行的,因此,如果在一行中找到匹配项,它将在输出中包含整行。 Assuming that's what you really want, you can just or the two patterns together: 假设这是你真正想要的,你可以直接or两种模式一起:

grep ("My name is" | "my bank account")

Given the input above, this should produce: 鉴于以上输入,应产生:

My name is Mike. I'm 16 years old.
you some money from my bank account. 

Alternatively, since you haven't included any meta-characters in your patterns, you could use fgrep (or grep -F) and put your patterns in a file, one per line. 另外,由于您的模式中未包含任何元字符,因此可以使用fgrep(或grep -F)并将模式放在文件中,每行一个。 For two patterns this probably doesn't make a big difference, but if you want to look for lots of patterns, it'll probably be quite a bit faster (it uses the Aho-Corasick string search to search for all the patterns at once instead of searching for them one at a time). 对于两种模式,这可能并没有太大的区别,但是如果您要查找很多模式,则可能会快很多(它使用Aho-Corasick字符串搜索来一次搜索所有模式而不是一次搜索一个)。

The other possibility would be that you're looking for a single line that includes both my name is and my bank account . 另一种可能是您要查找包含my name ismy bank account一行。 That's what @djechlin's answer would do. 这就是@djechlin的答案。 From the input above, that would produce no output, so I doubt it's what you want, but if it is, his answer is fairly reasonable. 从上面的输入中,将不会产生任何输出,因此我怀疑这就是您想要的,但是如果是这样,他的答案是相当合理的。 An alternative would be a pattern like ("My name is.*my bank account" | "my bank account.*My name is") . 另一种可能是类似("My name is.*my bank account" | "my bank account.*My name is")

Yes. 是。 It is possible. 有可能的。 I used sed. 我用sed。 You can replace S1 and S2 with whatever you want 您可以用任何您想要的替换S1和S2

sed '/S1/{ s:.*:S1:;H};/S2/{ s:.*:S2:;H};${x;s:\n: :g;p};d' 

Sed is much more complex than grep, and in this case I used it to simulate grep's behaviour that you wish. Sed比grep复杂得多,在这种情况下,我使用它来模拟您希望的grep行为。

If you do not care about a trailing newline, simply use grep : 如果您不关心尾随换行符,只需使用grep

< file.txt grep -o "My name is\|my bank account" | tr '\n' ' '

If you would prefer a trailing newline, use awk : 如果您希望尾随换行符,请使用awk

awk -v RS="My name is|my bank account" 'RT != "" { printf "%s ", RT } END { printf "\n" }' file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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