bash egrep AND 运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15177287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:24:29  来源:igfitidea点击:

egrep AND operator

bashgrep

提问by Bob

I know egrep has a very useful way of anding two expressions together by using:

我知道 egrep 有一种非常有用的方法,可以使用以下方法将两个表达式结合在一起:

egrep "pattern1.*pattern2"|egrep "pattern2.*pattern1" filename.txt|wc -l

However is there an easy way to use egrep's AND operator when searching for three expressions as the permutations increase exponentially as you add extra expressions.

但是,在搜索三个表达式时,是否有一种简单的方法可以使用 egrep 的 AND 运算符,因为随着您添加额外的表达式,排列会呈指数增长。

I know the other way going about it using sort|uniq -dhowever I am looking for a simpler solution.

我知道使用的另一种方法,sort|uniq -d但是我正在寻找一个更简单的解决方案。

EDIT:

编辑:

My current way of search will yield five total results:

我目前的搜索方式将产生五个总结果:

#!/bin/bash
pid=$$
grep -i "angio" rtrans.txt|sort|uniq|egrep -o "^[0-9]+ [0-9]+ " > /tmp/$pid.1.tmp
grep -i "cardio" rtrans.txt|sort|uniq|egrep -o "^[0-9]+ [0-9]+ " > /tmp/$pid.2.tmp
grep -i "pulmonary" rtrans.txt|sort|uniq|egrep -o "^[0-9]+ [0-9]+ " > /tmp/$pid.3.tmp
cat /tmp/$pid.1.tmp /tmp/$pid.2.tmp|sort|uniq -d > /tmp/$pid.4.tmp
cat /tmp/$pid.4.tmp /tmp/$pid.3.tmp|sort|uniq -d > /tmp/$pid.5.tmp
egrep -o "^[0-9]+ [0-9]+ " /tmp/$pid.5.tmp|getDoc.mps > /tmp/$pid.6.tmp
head -10 /tmp/$pid.6.tmp

mumps@debianMumpsISR:~/Medline2012$ AngioAndCardioAndPulmonary.script 
1514 Structural composition of central pulmonary arteries. Growth potential after surgical shunts.
1517 Patterns of pulmonary arterial anatomy and blood supply in complex congenital heart disease
with pulmonary atresia
3034 Controlled reperfusion following regional ischemia.
3481 Anaesthetic management for oophorectomy in pulmonary lymphangiomyomatosis.
3547 A comparison of methods for limiting myocardial infarct expansion during acute reperfusion--
primary role of unload

While:

尽管:

mumps@debianMumpsISR:~/Medline2012$ grep "angio" rtrans.txt|grep "cardio" rtrans.txt|grep "pulmonary" rtrans.txt|wc -l
185

yields 185 lines of text because it is only taking the value of the search in pulmonary instead of all three searches.

产生 185 行文本,因为它只采用肺中的搜索值,而不是所有三个搜索。

回答by Kent

how about

怎么样

grep "pattern1" file|grep "pattern2"|grep "pattern3" 

this will give those lines that contain p1, p2 and p3. but with arbitrary order.

这将给出包含 p1、p2 和 p3 的那些行。但有任意顺序。

回答by Stanislav

The approach of Kent with

肯特的方法与

grep "pattern1" file|grep "pattern2"|grep "pattern3" 

is correct and it should be faster, just for the record I wanted to post an alternative which uses egrepto do the same without pipping:

是正确的,它应该更快,只是为了记录,我想发布一个替代方案,它用于egrep在不使用的情况下执行相同的操作:

egrep "pattern1.*pattern2|pattern2.*pattern1"

which looks for p1 followed by p2or p2 followed by p1.

寻找p1 followed by p2or p2 followed by p1

回答by Lawrence

The original question is about why his egrep command didn't work.

最初的问题是关于为什么他的 egrep 命令不起作用。

egrep "pattern1.*pattern2"|egrep "pattern2.*pattern1" filename.txt|wc -l

Kent and Stanislav are correct in pointing out the syntax error by putting the filename.txt up front. But this doesn't address the original problem.

Kent 和 Stanislav 通过将 filename.txt 放在前面指出语法错误是正确的。但这并没有解决最初的问题。

Bob's "current way" (4 years ago) was a multi-command approach to grep out different keywords on different lines. In other words, his script was looking for a set of lines containing anyof his search terms. The other proposed solutions would only result in lines containing allof his search terms, which does not appear to be his intent.

Bob 的“当前方式”(4 年前)是一种多命令方法,用于在不同行中提取不同的关键字。换句话说,他的脚本正在寻找一组包含他的任何搜索词的行。其他提议的解决方案只会导致包含他所有搜索词的行,这似乎不是他的意图。

Instead, he could use a single line egrep to look for anyof the terms, like this:

相反,他可以使用单行 egrep 来查找任何术语,如下所示:

egrep -e 'pattern1|pattern2' filename.txt