LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep 2 items in a file (https://www.linuxquestions.org/questions/linux-newbie-8/grep-2-items-in-a-file-853425/)

noony123 12-31-2010 05:16 AM

grep 2 items in a file
 
Hi all.

I have collection my routers configuration through rancid. Now i want to do the following,

Display all the files that contain the following words

C1841 AND HWIC-4ESW

I want to display all the files that contains both these words.

Is it possible using grep ?

druuna 12-31-2010 05:27 AM

Hi,

Would this helps:

grep "C1841" * | grep -m1 "HWIC-4ESW"

The -m1 is there to stop searching if one match is found (this will prevent the printing of the same filename if the match occurs more then once).

Hope this helps.

noony123 12-31-2010 05:35 AM

Dear Sir,

the output of grep 'C1841' * doesnt give any line that contains HWIC-4ESW also. So its not working.

I read about agrep, i have installed it, but its and (;) operator is not working !

Kindly guide me pls

druuna 12-31-2010 05:48 AM

Hi,
Quote:

Originally Posted by noony123 (Post 4208625)
the output of grep 'C1841' * doesnt give any line that contains HWIC-4ESW also. So its not working.

I did assume that both words are on the same line, if that is not the case, try this:

grep -lm1 "HWIC-4ESW" $(grep -lm1 "C1841" *)

Hope this helps.

grail 12-31-2010 07:30 AM

Maybe awk may be able to help too:
Code:

awk 'FNR==1{x=0}/C1841|HWIC-4ESW/{x++}x==2{print FILENAME;nextfile}' *
Not tested, but you get the idea.

druuna 12-31-2010 07:48 AM

Hi,
Quote:

Originally Posted by grail (Post 4208691)
Maybe awk may be able to help too:
Code:

awk 'FNR==1{x=0}/C1841|HWIC-4ESW/{x++}x==2{print FILENAME;nextfile}' *
Not tested, but you get the idea.

Tested: If both words are on the same line, the above does not work, otherwise it will.

geovg 12-31-2010 08:58 AM

Quote:

Originally Posted by noony123 (Post 4208612)
Hi all.

I have collection my routers configuration through rancid. Now i want to do the following,

Display all the files that contain the following words

C1841 AND HWIC-4ESW

I want to display all the files that contains both these words.

Is it possible using grep ?

try

egrep 'C1841|HWIC-4ESW' filename

druuna 12-31-2010 09:06 AM

@geovg: That will find C1841 or HWIC-4ESW, not C1841 and HWIC-4ESW :)

grail 12-31-2010 09:35 AM

Well I will go back to my original line :)
Code:

awk 'FNR==1{x=0}/C1841/{x++}/HWIC-4ESW/{x++}x==2{print FILENAME;nextfile}' *

druuna 12-31-2010 09:49 AM

Quote:

Originally Posted by grail (Post 4208793)
Well I will go back to my original line :)
Code:

awk 'FNR==1{x=0}/C1841/{x++}/HWIC-4ESW/{x++}x==2{print FILENAME;nextfile}' *

And we have another winner ;)

I might even like this one better then mine: Just one command, although probably a bit more complicated for the novice.

noony123 01-01-2011 09:13 AM

I will check the awk command and will let you guys know about it. But what about agrep ? i have read that following can do

agrep 'C1841;HWIC-4ESW' filename

But i have been trying that with no success. Has anyone tried it ?

noony123 01-01-2011 09:14 AM

And really thank you for answering. I really owe you guys a lot

grail 01-01-2011 09:45 AM

agrep ... never heard of it before, but just googled it and there it was :doh: have no idea how to use it though :(

druuna 01-01-2011 11:11 AM

Hi,

I also never heard of agrep before you brought it up, so cannot help you with that one....
Quote:

And really thank you for answering.
You're welcome :)

ntubski 01-01-2011 12:00 PM

Code:

agrep 'C1841;HWIC-4ESW' filename
That will match lines with both C1814 and HWIC-4ESW on them, like druuna's first solution. You may be able to get what you want via creative use of the -d (delimiter) option: i.e. put as a delimiter something that doesn't show up in the file so it matches the entire file at once.


Code:

awk 'FNR==1{x=0}/C1841/{x++}/HWIC-4ESW/{x++}x==2{print FILENAME;nextfile}' *
@grail: that would match a file that has C1841 (or HWIC-4ESW) twice, how about:
Code:

awk 'FNR==1{C=H=0}/C1841/{C=1}/HWIC-4ESW/{H=1}C&&H{print FILENAME;nextfile}' *


All times are GMT -5. The time now is 08:23 PM.