LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-31-2010, 05:16 AM   #1
noony123
Member
 
Registered: Oct 2010
Posts: 167

Rep: Reputation: 0
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 ?
 
Old 12-31-2010, 05:27 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 12-31-2010, 05:35 AM   #3
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
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
 
Old 12-31-2010, 05:48 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,
Quote:
Originally Posted by noony123 View Post
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.

Last edited by druuna; 12-31-2010 at 06:06 AM. Reason: Added m1 part
 
1 members found this post helpful.
Old 12-31-2010, 07:30 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,471

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
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.
 
Old 12-31-2010, 07:48 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,
Quote:
Originally Posted by grail View Post
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.
 
Old 12-31-2010, 08:58 AM   #7
geovg
Member
 
Registered: Mar 2010
Posts: 40

Rep: Reputation: 1
Quote:
Originally Posted by noony123 View Post
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
 
Old 12-31-2010, 09:06 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
@geovg: That will find C1841 or HWIC-4ESW, not C1841 and HWIC-4ESW
 
Old 12-31-2010, 09:35 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,471

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
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}' *
 
Old 12-31-2010, 09:49 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by grail View Post
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.
 
Old 01-01-2011, 09:13 AM   #11
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
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 ?
 
Old 01-01-2011, 09:14 AM   #12
noony123
Member
 
Registered: Oct 2010
Posts: 167

Original Poster
Rep: Reputation: 0
And really thank you for answering. I really owe you guys a lot
 
Old 01-01-2011, 09:45 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,471

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
agrep ... never heard of it before, but just googled it and there it was have no idea how to use it though
 
Old 01-01-2011, 11:11 AM   #14
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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
 
Old 01-01-2011, 12:00 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,428

Rep: Reputation: 823Reputation: 823Reputation: 823Reputation: 823Reputation: 823Reputation: 823Reputation: 823
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}' *
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hi ,i have to grep a series say 98782 from a file containing series how can i grep it sushil.cool Linux - Newbie 6 05-25-2010 06:27 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
Creating Linked Lists from items in a file SeniorSE Programming 5 09-14-2005 10:53 PM
File Input, how to get number of items? ftgow Programming 1 03-11-2005 01:31 AM
terminal items to file g00ral Linux - General 2 05-09-2004 05:39 AM


All times are GMT -5. The time now is 11:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration