Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Scripting question involving grep and awk

Posted on 2003-02-19
5
Medium Priority
?
469 Views
Last Modified: 2013-12-27
I'm trying to put together a script that looks at the last line in a file named backQA1.log and pulls a filename.

EXAMPLE COMMAND OUTPUT:

# tail -1 back*
bdkadjxi anf  2003-02-19 18.08.26  0000-00-00 00.00.00  6  ...............  ALL          online_cons     util_file        ------ 6.10 (56)
#

(I believe this is all one line but it's getting wrapped)

The file name I am looking for in this instance is: bdkadjxi.anf, but as you can see from the output above it doesn't have the "." but has a space instead - and it has other garbage on the line I don't care about.

Now, once I have the filename I want to grep the file name I just discovered for errors.

EXAMPLE COMMAND OUTPUT:

# grep ERR bdkadjxi.anf
[4627.00] 21:34:40 ERR - File /oracle/QA1/sapdata11/dfkkopkd_5/dfkkopkd.data5 shrunk by 1025611264 bytes, padding with zeros
ERROR: executing command  /usr/openv/netbackup/bin/bpbackup -w -t 17 -S sugar -c sap_online_backup_qa1  -L /oracle/QA1/816_32/sapscript/backint_dir/.backint.log.3.1045620547 -k ".bdkadjxi.lst" -f /oracle/QA1/816_32/sapscript/backint_dir/.backint.files.1045620547.3 >> /oracle/QA1/816_32/sapscript/backint_dir/.backint.log.3.1045620547.output 2>&1
#
I would like this output emailed to me. If the grep ERR filename command does not return anything an email that says "Everything's Fine" would be cool.

Thanks in advance for any help you can offer!
Bill
0
Comment
Question by:Cyberfreak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 7985300
Here's a little script can do the job:

#!/bin/ksh
INFILE=/path-to/bdkadjxi.anf
TEMFILE/tmp/mytmpfile
MYEMAIL=fred@abc.com

rm $TEMFILE

grep ERR ${INFILE} > ${TEMFILE}

if [ -s ${TEMFILE} ] ; then
  mailx -s "Err messages" ${MYEMAIL} < ${TEMFILE}
else
  #nothing wrong !
  echo "Have a nice day" | mailx -s "Everything's Fine" ${MYEMAIL}
fi

rm $TEMFILE

exit 0
# End of script

modify the script with your email add, full path to the file.

Cheers!
 
0
 
LVL 38

Accepted Solution

by:
yuzh earned 400 total points
ID: 7985307
Typing mistake, it should be:

#!/bin/ksh
INFILE=/path-to/bdkadjxi.anf
TEMFILE=/tmp/mytmpfile
MYEMAIL="fred@abc.com"

rm $TEMFILE

grep ERR ${INFILE} > ${TEMFILE}
if [ -s ${TEMFILE} ] ; then
 mailx -s "Err messages" ${MYEMAIL} < ${TEMFILE}
else
 #nothing wrong !
 echo "Have a nice day" | mailx -s "Everything's Fine" ${MYEMAIL}
fi

rm $TEMFILE

exit 0
# End of script

modify the script with your email add, full path to the file.

Cheers!
 


0
 
LVL 48

Expert Comment

by:Tintin
ID: 7985365
To get the filename, you can do

filename=`tail -1 backQA1.log | awk '{print $1"."$2}'`

Then more or less follow yuzh's script.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 7985446
You need to put the full path  with the file name.

If you don't know it before hand, use "find" to locate it.
0
 

Author Comment

by:Cyberfreak
ID: 7988050
You rock dude!
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question