The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to grep word from .gz files & .z files udaya_subbu Shell Programming and Scripting 8 08-08-2008 02:53 AM
grep from .z files koho Shell Programming and Scripting 4 11-15-2007 10:55 PM
listing files along with grep -v charlie11k UNIX for Dummies Questions & Answers 5 06-01-2007 04:00 AM
modifying grep to get files only within last 2 hrs ragha81 Shell Programming and Scripting 3 01-11-2007 04:11 PM
grep data from files getdpg Shell Programming and Scripting 2 01-17-2006 09:57 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-04-2005
Registered User
 

Join Date: May 2005
Posts: 7
Script to grep several files

Hi I am totally new to Unix and I am in need of a script that can grep several files at once.
the script needed:
change directory
grep for keywords "error" "fail" "warning" in different files with different names, same extention but specific file names.
if any of the keywords is found to show the line number and the error

I usually run this grep manually every hour and it's time consuming, running a cron is not an option, so I would just copy and paste the script when needed.

Any help will be greatly appreciated.

Thanks,

Newby in need.
Reply With Quote
Forum Sponsor
  #2  
Old 05-04-2005
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
use -n option for grep.

Code:
grep -en "error|fail" *
Reply With Quote
  #3  
Old 05-04-2005
mohanprabu's Avatar
Registered User
 

Join Date: Nov 2004
Posts: 16
Hi Bhargav,

Is there any way to grep a keyword in all files - including those in the subdirectories as well. I usually use this method of coding:

for j in `find ./*`
do
grep <keyword> $j
done

But just out of curiosity - is there any other methods to perform the same.. which looks much simpler (instead of taking such multiple lines!!)
Reply With Quote
  #4  
Old 05-05-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Originally Posted by mohanprabu
But just out of curiosity - is there any other methods to perform the same.. which looks much simpler
find /path/here -type f -exec grep "whatever" {} /dev/null \;

Cheers
ZB
Reply With Quote
  #5  
Old 05-05-2005
Registered User
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
You can use find + xargs + grep to do this by one command as,

find <dir to be grepped> -name "*" | xargs grep -ne 'error|fail|warning'

It will do it.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0