The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #8 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: May 2009
Posts: 11
Hi meharo,

Thanks for your reply. How can we modify the code if we need to search for multiple files.
  #9 (permalink)  
Old 12-01-2009
Registered User
 

Join Date: Dec 2008
Posts: 13

Code:
 
#!/bin/bash
 
my_file1="/mylocation/myfile1.log"
my_file2="/mylocation/myfile2.log"
my_string="Match Found"
 
tail -F "$my_file1" "$my_file2" | while read line ; do
if test -n "$(echo "$line" | grep "$my_string")" ; then
echo "Match found!"
exit 0
else
exit 1
fi
done

The above program will wait for both file1 and file2. Whenever it finds a file with proper string match, condition triggers.

The else condition to quit the program (exit 1) has to be used wisely. If you are looking for some 'lock' files with specific string content, the program can wait for the file(s) to occur in specific path and act immediately. If the file is a log file and always present in path, it is not wise to use the else logic, it will just quit the program after reading the first line itself.

meharo
  #10 (permalink)  
Old 12-01-2009
Registered User
 

Join Date: Jul 2009
Location: Noida - India
Posts: 34
to recursively search a string in files in a dir try

find . -name <name / pattern of your file> -print | xargs grep <string to be searched> 2>/dev/null

Cheers
Sponsored Links
Reply

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search for string in a file and extract another string to a variable daikeyang Shell Programming and Scripting 6 03-20-2009 08:45 PM
How to search a string in a file. prathap UNIX for Dummies Questions & Answers 1 07-17-2007 03:41 AM
String search in log file sliver Shell Programming and Scripting 1 07-12-2006 08:09 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 09:53 AM
please help me to search string in a file ...... swamymns Shell Programming and Scripting 2 02-01-2006 12:20 PM



All times are GMT -4. The time now is 12:02 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