LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-18-2012, 10:56 AM   #1
pgb205
Member
 
Registered: Nov 2007
Posts: 129

Rep: Reputation: 15
Execute command when line matches


I'm monitoring a file with tail -f

tail -f <filename> |grep <string>

what i want to do is to execute another script when grep finds a new match.

Is there an event message that can be captured or some other method?
 
Old 07-18-2012, 11:05 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
if grep <string> filename 1>/dev/null; then
     <do stuff>
fi
 
Old 07-18-2012, 08:44 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Unfortunately, this seems to be an ever growing log file, so I don't think that would work because you'd have to know where you got to last time, then loop round and continue from that line num (not to mention logrotate issues).
I'd use http://search.cpan.org/~mgrabnar/Fil...0.99.3/Tail.pm instead, which is designed especially for this situation.
 
Old 07-18-2012, 11:45 PM   #4
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
Perhaps something like this...
Code:
while IFS= read var ; do

    # Some script here to parse the line stored in $var

done < <(tail -f tailfile | grep 'some pattern here')
 
Old 07-19-2012, 02:29 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
This is working on my system using GNU grep.
Code:
tail -f <filename> | grep --line-buffered <string> | while read;do
                                                       <scriptname>
                                                     done
Without the '--line-buffered' option the matched lines would be held back in a buffer instead of being sent immediately through the pipe to the while loop.
 
Old 07-20-2012, 11:38 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Or you could eliminate grep entirely.

Code:
while read -r; do

	[[ $REPLY == *string* ]] && ./scriptname

done < <( tail -f file )
 
  


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
print nth line after the line which matches the string cristalp Programming 7 10-27-2011 01:53 PM
need to delete a line if a field of that line matches using awf in bash scripting accesskarthi Linux - Newbie 8 06-29-2009 03:15 AM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 09:16 AM
PHP execute at command line gjmwalsh Mandriva 2 12-06-2003 01:03 AM
Can't execute ifconfig at command line jhsuan Linux - Newbie 3 08-30-2002 08:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration