Reading line by line from live log file using while loop and considering only those lines start from | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reading line by line from live log file using while loop and considering only those lines start from

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-15-2013
ketanraut ketanraut is offline
Registered User
 
Join Date: May 2013
Last Activity: 25 August 2014, 6:53 AM EDT
Posts: 29
Thanks: 5
Thanked 0 Times in 0 Posts
Reading line by line from live log file using while loop and considering only those lines start from

Hi,

I want to read a live log file line by line and considering those line which start from time stamp[2013-04-21 21:55:16,435];
Below code I am using, which read line but throws an exception when comparing line that does not contain error code

tail -F /logs/COMMON-ERROR.log | while read myline; do
ERROR_CODE=$(echo $ myline | awk -F ' ' '{for (i=1;i<=NF;i++){if($i ~ "^[1-1]" && length($i)==6){print $i}}}')
ERROR_CODE_DET=$(echo $ myline | awk -F ' ' '{print $1" "$2}')
echo "$ERROR_CODE_DET" "$ERROR_CODE"

if [ "$ERROR_CODE" -eq 100021 ] ; then
ERROR_MSG="Route failed. Moving to failed routes pool."
fi
done
----------------------------------------------------------------------------------------------
COMMON-ERROR.log:-
2013-10-13 11:56:26,429 | ERROR | NettyHandlers | 520002 Cache rolling back transaction: EXCEPTION: java.io.IOException: Connection reset by peer
2013-04-23 10:27:23,541 [New I/O client boss #1-0] | ERROR | RouteManager | 400021 Route failed. Moving to failed routes.
2013-05-06 22:53:29,000 [Thread-3] | ERROR | Routefailed | 400025 Failed to restart failed route
com.detica.ddsf.core.exception.InterfaceException: Error occured in interface 'IF07-1' :connection timed out
at com.detica.ddsf.modules.datainterfaces.HTTPDataInterfaces.initialise(HTTPDataInterfaces.java:249)
at com.detica.ddsf.core.load.Route.startRoute(Route.java:80)
at com.detica.ddsf.core.run.RouteManager.restartFailedRoutes(RouteManager.java:269)
at com.detica.ddsf.core.run.RouteManager$RestartDaemon.run(RouteManager.java:573)
at java.lang.Thread.run(Unknown Source)

NOTE :- Line containing error code 520002, 400025, 400021 will get compare with if condition and executed well but line without error code throw error
Ex:-
./new.sh: line 13: [: : integer expression expected
com.detica.ddsf.core.exception.InterfaceException: Error occured in interface 'IF07-1' :connection timed out
so I want to ignore these lines which throws error.
............

---------- Post updated at 01:17 PM ---------- Previous update was at 01:08 PM ----------

2013-10-13 11:56:26,429 | ERROR | NettyHandlers | 520002 Cache rolling back transaction: EXCEPTION: java.io.IOException: Connection reset by peer
2013-04-23 10:27:23,541 [New I/O client boss #1-0] | ERROR | RouteManager | 400021 Route failed. Moving to failed routes.
2013-05-06 22:53:29,000 [Thread-3] | ERROR | Routefailed | 400025 Failed to restart failed route
com.detica.ddsf.core.exception.InterfaceException: Error occured in interface 'IF07-1' :connection timed out
at com.detica.ddsf.modules.datainterfaces.HTTPDataInterfaces.initialise(HTTPDataInterfaces.java:249)
at com.detica.ddsf.core.load.Route.startRoute(Route.java:80)
at com.detica.ddsf.core.run.RouteManager.restartFailedRoutes(RouteManager.java:269)
at com.detica.ddsf.core.run.RouteManager$RestartDaemon.run(RouteManager.java:573)
at java.lang.Thread.run(Unknown Source)
Sponsored Links
    #2  
Old 10-15-2013
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 10 October 2014, 7:04 AM EDT
Location: ./India/Banglore
Posts: 1,207
Thanks: 53
Thanked 270 Times in 263 Posts
Hi Ketan,

1) Use code tag while posting your code.
2) You can check whether variable is zero length or not.

Code:
if [ -n "$ERROR_CODE" ] ; then
	if [ "$ERROR_CODE" -eq 100021 ] ; then
		ERROR_MSG="Route failed. Moving to failed routes pool."
	fi 
fi

The Following User Says Thank You to pravin27 For This Useful Post:
ketanraut (10-15-2013)
Sponsored Links
    #3  
Old 10-15-2013
ketanraut ketanraut is offline
Registered User
 
Join Date: May 2013
Last Activity: 25 August 2014, 6:53 AM EDT
Posts: 29
Thanks: 5
Thanked 0 Times in 0 Posts
Hi Pravin27,
thanks for quick reply, and its work good for me.
could you please tell me know, is tail -F option is good for reading live log file or is there any alternative to do same. And what its CPU/buffer utilization bcz I want to run it in background continuously for error check using 'nohup' command.

Regards,
Ketanr
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
While loop is not reading next line in the file when IF condition is used. vdurai Shell Programming and Scripting 4 02-20-2012 12:20 AM
How to start reading from the nth line till the last line of a file. machomaddy Shell Programming and Scripting 5 01-23-2012 01:16 PM
Read file using while loop not reading last line dgmm Shell Programming and Scripting 6 06-16-2011 04:37 AM
[Solved] Problem in reading a file line by line till it reaches a white line hakermania Shell Programming and Scripting 3 08-19-2010 11:37 AM



All times are GMT -4. The time now is 07:56 PM.