Search string and send email | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Search string and send email

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-20-2013
rockingvj rockingvj is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 7 April 2014, 4:32 PM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
IBM Search string and send email

Hi All,

I need script which should search for the "reason code" and it should send mail with its preceding line as well as the line below it.(first line before it and line after it)


I used below script but it is printing the whole lines inside it, your help is appreciated:

cd /app/usr/bin

grep -i 'reason code' system.log > queue_line.txt

( cat queue_line.txt ) | mailx -s "reason code" hello@gmail.com

system.log :
=========

Feb 13 21:47:14 app20 please enter the valid input. system cannot understnd the queue you entered
reason code 2085. from the default values.
The Create MQ Channel (CRTMQMCHL) command creates a new MQ channel definition, specifying those attributes that are to be different
Sponsored Links
    #2  
Old 02-20-2013
Vikram_Tanwar12 Vikram_Tanwar12 is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 21 November 2013, 3:32 AM EST
Posts: 168
Thanks: 13
Thanked 10 Times in 10 Posts
You can modify the
Code:
grep

code like this

Code:
grep -C 1 'reason code' system.log | grep -v 'reason code' > queue_line.txt

Sponsored Links
    #3  
Old 02-20-2013
mirwasim mirwasim is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 8 October 2013, 5:16 AM EDT
Posts: 105
Thanks: 8
Thanked 1 Time in 1 Post
Try this


Code:
count=`grep -n "reason code" $1 | awk '{print $1}' | cut -d":" -f 1`
prev_count=`expr $count - 1`
next_count=`expr $count + 1`
sed -n " $prev_count,$next_count p" $1 > queue_line.txt
cat queue_line.txt | mailx -s "reason code" hello@gmail.com

scriptname filename

Last edited by mirwasim; 02-20-2013 at 07:40 AM..
    #4  
Old 02-20-2013
gary_w's Avatar
gary_w gary_w is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 10 April 2014, 2:21 PM EDT
Posts: 446
Thanks: 32
Thanked 96 Times in 88 Posts
http://www.unix.com/shell-programmin...using-sed.html


Code:
 
sed -ne '/^reason code/{x;1!p;g;$!N;p;D;}' -e h x.dat | \
mailx -r "Reason_code_checker" -s "reason code" you@yourwork.com

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search a string and send the line containing it rockingvj Shell Programming and Scripting 14 11-06-2012 10:22 AM
Script to send email after comparing the folder permissions to a certain permission & send email nairshar Shell Programming and Scripting 2 12-07-2010 08:22 PM
Search for string and send mail AnneAnne Shell Programming and Scripting 1 03-05-2009 05:21 AM
need help in finding a string and to send an email using shell script ranga27 Shell Programming and Scripting 10 02-19-2008 04:54 PM
Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win) Vetrivela UNIX for Advanced & Expert Users 2 02-15-2005 09:43 AM



All times are GMT -4. The time now is 02:58 PM.