I know egrep has a very useful way of anding two expressions together by using:
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.
I know the other way going about it using sort|uniq -d
however I am looking for a simpler solution.
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.
how about
grep "pattern1" file|grep "pattern2"|grep "pattern3"
this will give those lines that contain p1, p2 and p3. but with arbitrary order.
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 egrep
to do the same without pipping:
egrep "pattern1.*pattern2|pattern2.*pattern1"
which looks for p1 followed by p2
or p2 followed by p1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With