Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Help in printing n number of lines if a search string matches in a file

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-02-2013   -   Original Discussion by neha0785
neha0785's Unix or Linux Image
neha0785 neha0785 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 2 April 2013, 2:46 PM EDT
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Help in printing n number of lines if a search string matches in a file

Hi
I have below script which is used to grep specific errors and if error string matches send an email alert.
Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert

Current code:-


Code:
#!/bin/bash
tail -Fn0 --retry /tmp/error.log | \
while read line ; do
        echo "$line" | egrep "java.lang.NullPointerException|java.lang.IllegalStateException|java.sql.SQLException"
        if [[ $? = 0 ]] ;
        then
HOSTNAME=abc.com
               SUBJECT="Error in "$HOSTNAME" Reported"
MESSAGE="/tmp/message.txt"
               TO="name@xyz.com"
echo "$line" > $MESSAGE
                /bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
fi
done

Appreciate any inputs.
I wish to keep the changes to code as minimal as possible.

Last edited by Scrutinizer; 04-02-2013 at 03:39 PM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 04-02-2013   -   Original Discussion by neha0785
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 12 December 2017, 12:49 PM EST
Location: Aachen, Germany
Posts: 11,788
Thanks: 339
Thanked 3,659 Times in 3,357 Posts
Please use code tags as required by forum rules!

One option, just dreamed up from the blue sky: Set up a counter, after the read, incremented by 0 until your grep matches. Then, increment by 1 and collect $line into the message file. When the counter hits 10, send the mail, reset increment to 0, clear the file, and continue.
Or, do the entire thing in e.g. awk.

BTW, you can take all the variable assignments out of the loop as it seems they remain constant.
Sponsored Links
    #3  
Old Unix and Linux 04-03-2013   -   Original Discussion by neha0785
hanson44's Unix or Linux Image
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 183 Times in 178 Posts
Don't do it in real time. Don't use tail -f which seems to me the problem.

Once an hour, or whatever, use tail to save the end part of the log file that has not been examined before. Save the end part to "tail.txt" for example. Use grep -C on tail.txt to get the context for each error line detected. Record total number of lines in the log file. Suppose there are 1000. Next time (in an hour), tail starts with line 1001 (or maybe 991 to make sure you have context).
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Printing 10 lines above and below the search string: help needed vimalm22 Shell Programming and Scripting 5 03-01-2010 02:22 AM
printing lines to a file from a particular string intiraju Shell Programming and Scripting 4 02-24-2010 05:07 PM
Get line number when matches a string Muthuraj K Shell Programming and Scripting 4 02-02-2010 09:06 AM
question about printing number of lines in a file a8111978 UNIX for Dummies Questions & Answers 5 09-17-2002 11:15 AM



All times are GMT -4. The time now is 01:49 PM.