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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading line by line from live log file using while loop and considering only those lines start from
# 1  
Old 10-15-2013
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.
......Smilie......

---------- 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)
# 2  
Old 10-15-2013
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

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 10-15-2013
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Problem with while loop reading every line of a text file

Hello, I'm using RHEL 5.1 with bash. How to handle "read" inside while loop reading every line? Please see below: # cat /tmp/passwd_sample CARRJ12:qVSn4ja4mFA72,..:20021:125:JULIAN CARR:/home/everyone:/bin/bash HERCOT01:NK/3j2ZB4ZC7Q:20022:125:TOM HERCOCK:/home/everyone:/bin/bash... (4 Replies)
Discussion started by: reddyr
4 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

How to process only new line of tail -f command from live log file?

Hi, I want to read a live log file line by line and considering those line which are newly added to file Below code I am using, which read line but as soon as it read new line from log file its starts processing from very first line of file. tail -F /logs/COMMON-ERROR.log | while read... (11 Replies)
Discussion started by: ketanraut
11 Replies

5. Shell Programming and Scripting

Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format: I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one. Any help would be greatly appreciated. (10 Replies)
Discussion started by: nwalsh88
10 Replies

6. Shell Programming and Scripting

While loop is not reading next line in the file when IF condition is used.

Hi Guys I am new to scripting.Please forgive for asking basic questions. I want to write a script to check whether the logs are getting updated in last 15 mins. cat server 192.168.1.6 192.168.1.7 cat list 192.168.1.7 /logs/logpath1 192.168.1.7 /logs/logpath2 192.168.1.6... (4 Replies)
Discussion started by: vdurai
4 Replies

7. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

8. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

9. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies
Login or Register to Ask a Question