Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 17th, 2009, 04:53 PM
s3rro s3rro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 15 s3rro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 11 m 39 sec
Reputation Power: 0
Grep multiple lines from a text file

I have a list of words that I want to grep in many files to see which ones have it and which ones dont.

in the text file I have all the words listed line by line, ex:

list.txt:

check
try this
word1
word2
open space
list ..

I want to grep each line one by one.
like I want it to

grep "check" *.log
grep "try this" *.log
grep "word1" *.log .. etc how can I do this?

and maybe write the output to a file.

Thanks

Reply With Quote
  #2  
Old June 17th, 2009, 05:01 PM
menator's Avatar
menator menator is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2001
Posts: 594 menator User rank is Sergeant (500 - 2000 Reputation Level)menator User rank is Sergeant (500 - 2000 Reputation Level)menator User rank is Sergeant (500 - 2000 Reputation Level)menator User rank is Sergeant (500 - 2000 Reputation Level)menator User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 4 h 9 m 46 sec
Reputation Power: 25
Send a message via MSN to menator
One pssibility would be to write a shell script looping through an array of words

Reply With Quote
  #3  
Old June 17th, 2009, 09:47 PM
Bardon Bardon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 39 Bardon User rank is Corporal (100 - 500 Reputation Level)Bardon User rank is Corporal (100 - 500 Reputation Level)Bardon User rank is Corporal (100 - 500 Reputation Level)Bardon User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 21 h 26 m 25 sec
Reputation Power: 6
Quote:
Originally Posted by s3rro
I have a list of words that I want to grep in many files to see which ones have it and which ones dont.

in the text file I have all the words listed line by line, ex:

list.txt:

check
try this
word1
word2
open space
list ..

I want to grep each line one by one.
like I want it to

grep "check" *.log
grep "try this" *.log
grep "word1" *.log .. etc how can I do this?

and maybe write the output to a file.

Thanks


A simple script like this should do it:
Code:
cat $1 | while read search
do
 grep "$search" *.log >> output.file
done


Note that you'll need to clear the output file if you re-run this. There's a lot more you can do with this, but it's a stub to get you started.

Reply With Quote
  #4  
Old June 18th, 2009, 08:42 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2006
Posts: 1,043 SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level)SimonJM User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 3 Weeks 1 Day 17 h 8 m 56 sec
Reputation Power: 628
I'd suggest using the -l (ell) option to just list the filename in which the match occurs.
__________________
"I feel so miserable without you; it's almost like having you here" - Stephen Bishop

Reply With Quote
  #5  
Old June 18th, 2009, 09:31 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,139 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 1 Day 23 h 22 m 21 sec
Reputation Power: 861
Or maybe even just read the manual
Quote:
Originally Posted by grep manual
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file con-
tains zero patterns, and therefore matches nothing.


As in
grep --file=list.txt *.log
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Reply With Quote
  #6  
Old June 20th, 2009, 06:44 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,107 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 6 Days 13 h 23 sec
Reputation Power: 10
Quote:
Originally Posted by Bardon
A simple script like this should do it:
Code:
cat $1 | while read search
do
 grep "$search" *.log >> output.file
done


Note that you'll need to clear the output file if you re-run this. There's a lot more you can do with this, but it's a stub to get you started.



RTFM: man [ef]grep (mybe also: man exec)
__________________
working on Solaris[5-9], preferred languages french and C.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Grep multiple lines from a text file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
Stay green...Green IT