The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 06:53 AM
grep from .z files koho Shell Programming and Scripting 4 11-16-2007 01:55 AM
listing files along with grep -v charlie11k UNIX for Dummies Questions & Answers 5 06-01-2007 08:00 AM
modifying grep to get files only within last 2 hrs ragha81 Shell Programming and Scripting 3 01-11-2007 07:11 PM
grep data from files getdpg Shell Programming and Scripting 2 01-17-2006 12:57 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-05-2005
sqloyd sqloyd is offline
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.
  #2 (permalink)  
Old 05-05-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
  
 

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

Code:
grep -en "error|fail" *
  #3 (permalink)  
Old 05-05-2005
mohanprabu's Avatar
mohanprabu mohanprabu is offline
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!!)
  #4 (permalink)  
Old 05-05-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #5 (permalink)  
Old 05-05-2005
muthukumar muthukumar is offline
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.
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0