need help in Grep a string in the log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help in Grep a string in the log file
# 1  
Old 02-05-2013
need help in Grep a string in the log file

Code:
logfile="/var/tmp.log"
output=$(grep "ERROR" $logfile)
if [ -n $output ]
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 expected, I am trying to grep the error string and send an email if the string "error" is present on the log file, could you please help in this?

Last edited by vbe; 02-05-2013 at 12:00 PM..
# 2  
Old 02-05-2013
What is not working?
We cant really help you based on what you gave us so far...
Did you get an error in your script?
What is the content of $output? etc....
# 3  
Old 02-05-2013
thanks for the quick reply
Code:
: [: too many arguments
+ echo 'not found'
not found

The issue is if [ -n $output ] getting as "too many arguments"

When i try to run the script, It was able to find the "error" (saying too many arguments) and finally its says not found, even though the log file has the matches strings

Last edited by Franklin52; 02-06-2013 at 06:19 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 02-05-2013
Hi
try to enclose the variable with double quotes:

if [ -n "$output" ]
....Smilie
This User Gave Thanks to franzpizzo For This Post:
# 5  
Old 02-05-2013
Or try:
Code:
logfile="/var/tmp.log"
if grep -q "ERROR" "$logfile"
then
        echo "Exceptions logged, please check ${logfile} for more details" | mail -s "Exceptions logged" $DL
else
        echo "Not found"
fi

This User Gave Thanks to Yoda For This Post:
# 6  
Old 02-05-2013
Thank you both!!
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

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

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: eb222
4 Replies
Login or Register to Ask a Question