how to grep for string in log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to grep for string in log file
# 1  
Old 11-22-2007
how to grep for string in log file

Hi

Im running a backup scriptwhich creates a log file

how do grep for the string in the logfile so the backup script can continue to next stage otherwise it will exit

i.e

12:32:53 INF - Client completed sending data for backup

12:33:02 INF - Backup by root on client lonbob04bak using policy Business_Objects_User, sched bus_obj_user: the requested operation was successfully completed.

so want to have something like:


if

[logfile | grep "the requested operation was successfully completed"]

then continue
# 2  
Old 11-22-2007
Code:
if [ `grep "the requested operation was successfully completed" ${LOGFILE}` ]
then
  ... all ok ...
else
  ... gone south ...
fi

Off the cuff, so please test yourself. (just to be sure) Smilie

Last edited by Cameron; 11-22-2007 at 10:13 AM.. Reason: missed a '`'
# 3  
Old 11-22-2007
or maybe... u could

grep "the requested operation was successfully completed" logfile > /dev/null
if [ $? -eq 0 ]
then
continue
else
exit
fi

-
# 4  
Old 11-22-2007
script

#!/bin/ksh

grep "the requested operation was successfully completed" $1 >/dev/null
RESULT =`echo $?`
if [ $RESULT == 0 ]; then
echo "Continue"
else
echo "Stop"
fi

Assume this script file name is sample.sh. If your log file name is logfile, then in the command prompt give like this

$sample.sh logfile
# 5  
Old 11-22-2007
There are several approaches depending on:
- the backup script runs outside your script.
- the backup script finishes when that line is shown inside the log.
- the backup log only has (or will have) one line containing the text.
- others... Smilie
One possibility in this case:
Code:
#!/bin/ksh

( tail -f backup.log | while read l; do
   echo ".\c"
   echo $l | grep "the requested operation was successfully completed" > /dev/null 2>&1
   (( ! $? )) && exit 0
done ) && echo "string found, continue..."

# whatever to execute after the match, down here...

At least if you want to check the log file "on the fly"...

Regards.

Last edited by grial; 11-22-2007 at 01:21 PM.. Reason: comment added
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Script to grep for a string in log files generated in last 15 minutes.

Dear Guru's I've a requirment to grep for a string in series of log files that are getting generated almost every minute. I'm looking to schedule a script every 15 mountes,in order to check if the error string has been generated in any of the log files generated in last 15 minutes. Please... (3 Replies)
Discussion started by: rajivatnova
3 Replies

3. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies

4. Shell Programming and Scripting

need help in Grep a string in the log file

logfile="/var/tmp.log" output=$(grep "ERROR" $logfile) if then echo "exceptions logged , please check /var/tmp.log for more details" |\ mail -s "exceptions logged , please check /var/tmp.log on for more details" $DL else echo "not found" fi Somehow its not working as... (5 Replies)
Discussion started by: ajothi
5 Replies

5. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV. The strings could be on different line numbers, depending on size of log. Example Log File: File = foo.bat Date = 11/11/11 User = Foo Bar Size = 1024 ... CSV should look like: "foo.bat","11/11/11","Foo Bar","1024" (7 Replies)
Discussion started by: chipperuga
7 Replies

8. Programming

Help to grep string in log files

hello guys.., i have some problem with grepping strings in log files.. so..,i need an java logic on how to grep srtings in log files the output must be in gui.., please help me .. regards raghuraipur:confused: (1 Reply)
Discussion started by: raghuraipur
1 Replies

9. Shell Programming and Scripting

If else - grep a string in a file

Hi all, I would want to recieve notification only if there are sessions block in our databases. This is my script, but it's not working. No matter what is the contents in the $OUTFILE, I get emails. /usr/bin/cat $OUTFILE | read selected if then echo "No session is blocked" else ... (7 Replies)
Discussion started by: *Jess*
7 Replies
Login or Register to Ask a Question