1834027 Members
6765 Online
110063 Solutions
New Discussion

grep files

 
SOLVED
Go to solution
peterchu
Super Advisor

grep files

If I use grep "abc" ./* , it only grep the files that contains "abc" , if I want to add one more conditions , for example , the files contains "abc" AND "def" , how to do it ? thx.
5 REPLIES 5
Dave Olker
Neighborhood Moderator
Solution

Re: grep files

You can use egrep, or grep -E:

# egrep "abc|def" ./*

or

# grep -E "abc|def" ./*

Regards,

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Muthukumar_5
Honored Contributor

Re: grep files

We can do it by using extended regular expresssions with -E option on grep or with egrep command.

If you want to get the files contents which contain anyof the pattern "abc" or "def" then use

grep -E "abc | def" ./*
egrep -i "abc | def" ./*

-i used to ignore the case type

And more we can do it with multiple -e option as,

grep -ie abc -ie def filename

It will do grep abe / def.

If you want to print only the lines with abc and def then,

grep -E 'abc' filename | grep -E 'def'

It will do that.

If you want to use patterns with - then use -e option

Regards
Muthu
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

Re: grep files

I think the answer is:

grep -l abc * | xargs grep -l def

Like Muthu writes, the established way of finding a file with both abc AND def ON ONE LINE is to chain greps:

grep abc * | grep def

or, if you know def follows abc:

grep -E "abc.*def" *

or, I suppose you could use a convoluted:

grep -E "abc.*def|def.*abc" *


However, I think the author wants to list filenames for those fiels that contain both 'abc' and 'def', but not necesarilly on one line.

So use: grep -l abc x* | xargs grep -l def

The first grep creates a list of filenames that contain abc, then xargs combines that list into agruments fro a second grep which again reads the whole file, not just the lines with 'abc'.


Bonus answer... in AWK:

awk '//{if (FNR==1){a=d=x=0}} /abc/{a++} /def/{d++} {if (a&&d&&!x++) {print FILENAME}}' *

And in PERL

perl -e 'while ($f=shift @ARGV){$a=$d=0;open (F,"<$f");while (){$a++ if /abc/; $d++ if /def/} print "$f\n" if ($a&&$d)}' *

Both clear flags for each condition when a new file is started (in AWK we use FNR=1).
AWK limits printing FILENAME to once through a flag. PERL only tests at the end of the current file. Perl is easier to read (imho), but a 'lot' longer to write.

Grins,
Hein







However, I think you are lookinf
Petr Simik_1
Valued Contributor

Re: grep files

Easiest

grep -e cond1 -e cond2 -e cond3 -e cond file*

Hein van den Heuvel
Honored Contributor

Re: grep files


Petr,

Please help me understand how your solution would solve the problem stated: an AND condition.

Regards,
Hein.