LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-06-2010, 11:25 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 525

Rep: Reputation: 32
How to do "grep" on each file in directory


Hello,

Can someone show me how I can do the following in a bash script?
1) Load all *.log files into an array variable
2) Iterate through the array, and do a "grep" on each of the file to see if it contains a "word".
 
Old 07-06-2010, 11:35 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by grob115 View Post
Hello,

Can someone show me how I can do the following in a bash script?
1) Load all *.log files into an array variable
2) Iterate through the array, and do a "grep" on each of the file to see if it contains a "word".
Why not simply

grep PATTERN *.log

?
 
Old 07-06-2010, 06:32 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,413

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
I am with Sergei, I feel you are over thinking your solution.
 
Old 07-06-2010, 08:42 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,057

Rep: Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971Reputation: 971
C'mon guys, bash now supports associative arrays - wouldn't be too difficult to do ....
 
Old 07-06-2010, 09:46 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
This sounds like a homework question. If you want further help, you should post what you have tried. Look in the bashref manual and read through section 6.7 for information about using array variables.
 
Old 07-07-2010, 04:34 AM   #6
grob115
Member
 
Registered: Oct 2005
Posts: 525

Original Poster
Rep: Reputation: 32
No this is not a homework question. Unfortunately I can't recall the reason why I was thinking of doing it this way. I have moved on to other projects, and will need to re-visit this at another time. Will post here what I've tried previously.


However, I think it's because of the reason I want to see what it had grepped on what file. So I think I've echoed the file name first, with `grep <word> $filename[count]` ...... Can't remember the exact syntax. But the grep result overwrites part of the echoed file name.
 
Old 07-07-2010, 04:37 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by grob115 View Post
...
However, I think it's because of the reason I want to see what it had grepped on what file. So I think I've echoed the file name first, with `grep <word> $filename[count]` ...... Can't remember the exact syntax. But the grep result overwrites part of the echoed file name.
(Re)read 'man grep' - 'grep' has a command line switch allowing to see the file in which a match occurs.
 
Old 07-07-2010, 04:39 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
And consider 'find' with '-exec'.
 
Old 07-07-2010, 05:16 AM   #9
rkelsen
Senior Member
 
Registered: Sep 2004
Distribution: slackware
Posts: 1,748

Rep: Reputation: 159Reputation: 159
grep -r "word" /directory

- or -

for i in `ls /directory`; do grep "word" $i; done
 
  


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
"bad interpreter : no such file or directory" when configure "flex" acer_peri Linux - Software 10 11-10-2010 01:19 AM
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 04:37 PM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
"grep: /etc/issue: No such file or directory" hunter_one Linux - Software 6 10-15-2003 03:27 PM


All times are GMT -5. The time now is 12:04 AM.

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