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.
......:confused:......

---------- 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)

Hi Ketan,

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

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

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