LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to find a specific word between two lines having same another word.. (https://www.linuxquestions.org/questions/programming-9/how-to-find-a-specific-word-between-two-lines-having-same-another-word-4175458458/)

say_hi_ravi 04-17-2013 07:04 AM

How to find a specific word between two lines having same another word..
 
Hi,

First of all apologies for that poor statement I wrote - my bad english :-(

Let me elaborate in example what I exactly need.

I have a java app log file containing different exceptions.

Code:

$ vi java.log

2013-01-23 21:19:32,621 INFO [STDOUT] (my-nio-server-WorkerThread-4029) [LCDS]01/23/2013 GatewayEndpoint 'gateway-endpoint' failed to process a synchronized authentication.
flex.messaging.security.SecurityException: External login command required. Please check your security configuration.
at flex.messaging.security.LoginManager.login(LoginManager.java:245)
at flex.messaging.endpoints.GatewayEndpoint$GatewayEndpointProtocolHandler.processAuthenticationSyncOperation(GatewayEndpoint.java:874)
at flex.messaging.endpoints.GatewayEndpoint$GatewayEndpointProtocolHandler.processReceivedGatewayMessage(GatewayEndpoint.java:836)
at flex.messaging.endpoints.GatewayEndpoint$GatewayEndpointProtocolHandler.processReceivedMessage(GatewayEndpoint.java.exception:779)
at flex.messaging.endpoints.AMFSocketProtocolHandler.processReceivedMessageChunk(AMFSocketProtocolHandler.java:158)
at flex.messaging.io.amfsocket.AMFSocketInput.read(AMFSocketInput.java:139)
at flex.messaging.endpoints.AMFSocketProtocolHandler.doRead(AMFSocketProtocolHandler.java:132)
2013-01-23 22:30:44,044 INFO [org.apache.axis2.transport.http.HTTPSender] (http-0.0.0.0-8080-9) Unable to sendViaPost to url[https://www.zuora.com/apps/services/a/43.0]
org.apache.commons.httpclient.ConnectionPoolTimeoutException: Timeout waiting for connection
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.doGetConnection(MultiThreadedHttpConnectionManager.java:497)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.getConnectionWithTimeout(MultiThreadedHttpConnectionManager.java:416)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:153)
at org.apache.commons.httpclient.HttpClient.execute(HttpClient.java.exception:397)
at org.apache.axis2.transport.http.AbstractHTTPSender.executeMethod(AbstractHTTPSender.java:621)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:193)
2013-01-23 22:30:44,044 INFO [org.apache.axis2.transport.http.HTTPSender] (http-0.0.0.0-8080-9) Unable to sendViaPost to url[https://www.zuora.com/apps/services/a/43.0]
org.apache.commons.httpclient.ConnectionPoolTimeoutException: Timeout waiting for connection
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.doGetConnection(MultiThreadedHttpConnectionManager.java:497)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.getConnectionWithTimeout.exception(MultiThreadedHttpConnectionManager.java:416)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:153)

I want to find how many "exception" words are there between line having "INFO" word in them.

ie. for example, in below section there are two exception errors..

Code:

2013-01-23 21:19:32,621 INFO [STDOUT] (my-nio-server-WorkerThread-4029) [LCDS]01/23/2013 GatewayEndpoint 'gateway-endpoint' failed to process a synchronized authentication.
flex.messaging.security.SecurityException: External login command required. Please check your security configuration.
at flex.messaging.security.LoginManager.login(LoginManager.java:245)
at flex.messaging.endpoints.GatewayEndpoint$GatewayEndpointProtocolHandler.processAuthenticationSyncOperation(GatewayEndpoint.java:874)
Unable to sendViaPost to url[https://www.zuora.com/apps/services/a/43.0]
org.apache.commons.httpclient.ConnectionPoolTimeoutException: Timeout waiting for connection
2013-01-23 22:30:44,044 INFO [org.apache.axis2.transport.http.HTTPSender] (http-0.0.0.0-8080-9) Unable to sendViaPost to url

Thanks in advance.

Regards,
Ravi

danielbmartin 04-17-2013 07:15 AM

Quote:

Originally Posted by say_hi_ravi (Post 4933100)
I want to find how many "exception" words are there between line having "INFO" word in them.

Please provide a sample output file which corresponds to the sample input file already posted. That, along with your words, will help us to understand your goal.

Daniel B. Martin

David the H. 04-18-2013 02:47 PM

Perhaps something like this?

Code:

awk '/INFO/ { count[++n]=0 } ; /Exception/ { count[n]++ } ; END{ for (i in count){ printf "section %d has %d exceptions\n",i,count[i] } }' | sort -k1n

danielbmartin 04-18-2013 06:12 PM

Quote:

Originally Posted by David the H. (Post 4934257)
Perhaps something like this?

Code:

awk '/INFO/ { count[++n]=0 } ; /Exception/ { count[n]++ } ; END{ for (i in count){ printf "section %d has %d exceptions\n",i,count[i] } }' | sort -k1n

I like this solution, and offer a minor improvement...
Code:

awk '/INFO/ {count[++n]=0}; /Exception/ {count[n]++};
  END{for (i=1;i<=n;i++)
  {printf "section %d has %d exceptions\n",i,count[i]}}' $InFile >$OutFile

...which produces the same result without needing the sort step.

Daniel B. Martin

David the H. 04-18-2013 06:37 PM

Yeah, I should've thought of that. I hate having to fight with awk's lack of easy array control and tend towards the laziest approach. awk really needs to get a few built-in array and field range functions; splicing and sorting can be so annoying.


All times are GMT -5. The time now is 10:49 PM.