Code to search for a phrase and run a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code to search for a phrase and run a command
# 1  
Old 07-31-2012
Code to search for a phrase and run a command

hi All,

Am newbie to unix and would require your help to complete this task.

Condition:
I have a server start up script and I would like to append the code in such a way that it searches for a phrase "process completed" in taillog when server starts up and also should run another script abc.sh as soon as it sees the above message(process completed).
OS: SunOS 5.10 Generic_142900-14 sun4u sparc SUNW,Sun-Fire-V245

Thanks in Advance.
# 2  
Old 07-31-2012
So it needs to continuously monitor a growing file?
# 3  
Old 07-31-2012
No, not a monitoring thing, I want to put this piece in server script, such that whenever server is restarted, while in restart process, this code should search for "process completed" phrase in taillog and when it finds that phrase, should run one more script xxx.sh from a particular location.
# 4  
Old 07-31-2012
Looking for something like this? This works on Linux, can't confirm the same on SunOS.

Code:
find . -name taillog -exec grep -q 'process completed' {} \; -exec ./xxx.sh  \;

# 5  
Old 07-31-2012
for some reason above statement which mjf sent isn't working, as taillog is not a file, it is a log file. I somehow managed to google and found this one.

tail -f /a/b/c/xyz.log | grep 'process completed'

above statement was successful as I just tested with my requirements. So I am looking for second part which means a command should run a script as soon as it finds 'process completed' phrase in xyz.log.
# 6  
Old 07-31-2012
Use awk instead of grep which then can exit when it finds the desired string:

Code:
set -o pipefail
tail -f /a/b/c/xyz.log | awk '/process completed/ { exit( 0 ) }'
if (( $? != 0 ))
then
   echo "abort: tail or awk failed"
   exit 1
fi
echo "execute desired comand"

The awk will "block" and exit when it finds the desired string. After that you should check the return code and then can run your command if the tail/awk pipe was successful.

---------- Post updated at 21:52 ---------- Previous update was at 21:48 ----------

You could also use the -lq (lower case L) on grep which should have the same effect:

Code:
tail -f /path/file | grep -lq "process complete"

Awk always springs to mind first for me!
# 7  
Old 07-31-2012
hi agama,

got one more question here, as my xyz.log file keep changing daily as it has date stamp in filename just like xyz073112.log(as of today). So can we use /a/b/c/taillog.sh in first step in above code. Any means we can use taillog.sh to get rid of daily updated xyzxxx.log file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

When I run the code yesterday I am getting output,when I run same code today I am getting error?

If run the below code today its creating all directory and getting output files,I f run same code tomorrow I am getting error. can any one give suggestion to sortout this error. OSError: no such file or directory : '062518'My code looks like this import paramiko import sys import os ... (8 Replies)
Discussion started by: haribabu2229
8 Replies

2. Shell Programming and Scripting

How to find a phrase and pull all lines that follow until the phrase occurs again?

I want to burst a report by using the page number value in the report header. Each section starts with *PAGE NO:* 1 Each section might have several pages, but the next section always starts back at 1. So I want to find the "*PAGE NO:* 1" value and pull all lines that follow until "*PAGE NO:* 1"... (4 Replies)
Discussion started by: Scottie1954
4 Replies

3. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

4. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

5. Shell Programming and Scripting

Run breath-first-search find

I am trying to look for a certain file in multiple folders. When I hit the file, I want to stop going to subdirectory. For example: /foo/.target /bar/buz/.target /foo/bar/.target I want only /foo/.target /bar/buz/.target Any ideas how:)? (4 Replies)
Discussion started by: overmindxp
4 Replies

6. Shell Programming and Scripting

Is command line invocation of gnome-terminal to run more than one command possible?

Hello, I am trying to learn how to pass something more than a one-command startup for gnome-terminal. I will give an example of what I'm trying to do here: #! /bin/bash # #TODO write this for gnome and xterm USAGE=" ______________________________________________ ${0##*/} run... (0 Replies)
Discussion started by: Narnie
0 Replies

7. Shell Programming and Scripting

regarding about the (/) in the phrase

Hello, I am trying to print lines from a text file using this command gawk '/Filename:/' 11.rtf >> 22.rtf and it work ok. but if the phrase has included forward (/) something like that gawk '/File/name:/' 11.rtf >> 22.rtf it give error . so is there any manipulation when it... (1 Reply)
Discussion started by: davidkhan
1 Replies

8. Shell Programming and Scripting

Getting 15 characters after search phrase

I have data that looks like this: 2002 140 40800.0060 GPS 20 C1 25477810.2305 2002 140 41100.0060 GPS 20 C1 25298056.0453 I need to get data after certain pattern.. for example if i search for C1 it should return 25477810.2305 25298056.0453 To achieve this, it should: ... (3 Replies)
Discussion started by: bfr
3 Replies

9. Shell Programming and Scripting

convert phrase

I am converting a mysql database from myIsam to innodb. After dumping all databases to a file, I am trying to make modification: sed s/ENGINE=MyISAM/ENGINE=InnoDB/ dump_1 > dump_1_inno however, when I import the modified dump file to mysql server, I got error: ERROR 1214 (HY000) at line... (4 Replies)
Discussion started by: fredao
4 Replies

10. Shell Programming and Scripting

match a phrase

Hi, I have a these sentences. $sent1="Transactivation of wound-responsive genes containing the core sequence of the auxin-responsive element by a wound-induced protein kinase-activated transcription factor in tobacco plants."; $sent2="I branching formation in erythroid differentiation is... (4 Replies)
Discussion started by: vanitham
4 Replies
Login or Register to Ask a Question