scripting for error messages to grep in logs.

venu gopal
venu gopal used Ask the Experts™
on
dear ,

Hai this is venu from india

i want script for fetching error messages in log file for particular time , date and it will be save one file
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
venu,

You need to provide more information in order for someone to give you a useful answer... for insance:

1.  Which shell to use
2.  File name of log file(s) to search
3.  Where to save output file
4.  Will the script take command line input for parameters or will it prompt the user
5.  A sample log file will also be extremely valuable in order to formulate a solution
Distinguished Expert 2017
Commented:
how are the logs generated and by what applications. Look at sending your log events through syslog/rsyslog and have it process those logs and do what you need.
Additional suggestions to the ones provided by Jester.
Senior Manager Shell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT
Commented:
Sample command used:
#!/bin/bash
SAVE_ERROR_FILE=''`/bin/date "+%d_%b_%Y_%H_%M_%S_%N"`''"_Error_Message_From_Log.txt"
while [ 1 ]
do
	if test -f "$SAVE_ERROR_FILE"
	then
		/bin/sleep 1
		SAVE_ERROR_FILE=''`/bin/date "+%d_%b_%Y_%H_%M_%S_%N"`''"_Error_Message_From_Log.txt"
	else
		break
	fi
done
if test -f /tmp/Error_Log.txt
then
	/bin/egrep -i "error" /tmp/Error_Log.txt | /usr/bin/tee -a "$SAVE_ERROR_FILE"
	echo "Error output written in following file:"
	echo "$SAVE_ERROR_FILE"
else
	echo "/tmp/Error_Log.txt No such file" | /usr/bin/tee -a "$SAVE_ERROR_FILE"
fi

Open in new window

Above script will work for /bin/ksh or /bin/bash
Script written based on following assumption of log file content:
$ cat /tmp/Error_Log.txt
25/Apr/2000 01:00 AM PASS
25/Apr/2001 02:00 AM Error txt file not found
25/Apr/2002 03:00 AM PASS
25/Apr/2003 04:00 AM Error
25/Apr/2004 05:00 AM Error
25/Apr/2005 06:00 AM PASS
25/Apr/2006 07:00 AM Error
25/Apr/2017 01:00 AM PASS

Open in new window

Sample output:
$ /bin/chmod u+x ./CheckError_FromLog.sh
$ ./CheckError_FromLog.sh
25/Apr/2001 02:00 AM Error txt file not found
25/Apr/2003 04:00 AM Error
25/Apr/2004 05:00 AM Error
25/Apr/2006 07:00 AM Error
Error output written in following file:
25_Apr_2017_15_19_50_867731900_Error_Message_From_Log.txt

Open in new window

murugesandinsSenior Manager Shell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT

Commented:
Closing this query for
1. Question inactive
2. Tested and given resolved answer.
Comment if any objection.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial