473,699 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Match and Extract everything between 2 words in a file

37 New Member
I had posed a similar topic some time back but I want some additional information from the input file.
The log file is as shown...
Expand|Select|Wrap|Line Numbers
  1. [ERROR] [06 Apr 2009 09:45:56] [Trace] [WebContainer : 48] - App Number: 0
  2. [ERROR] [06 Apr 2009 09:45:56] [Trace] [WebContainer : 48] - Response for AE Completion No such RID Found
  3. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  4.     at sun.reflect.GeneratedMethodAccessor186.invoke(Unknown Source)
  5.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
  6.     at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
  7.     at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
  8.     at java.security.AccessController.doPrivileged1(Native Method)
  9.     at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code))
  10.     at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code))
  11.     at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.java(Compiled Code))
  12.     at $Proxy90.invoke(Unknown Source)
  13. nested exception is:
  14. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
  15.         at sun.reflect.GeneratedMethodAccessor186.invoke(Unknown Source)
  16.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
  17.     at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
  18.     at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
  19.     at java.security.AccessController.doPrivileged1(Native Method)
  20.     at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code))
  21.     at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code))
  22.     at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.java(Compiled Code))
  23.     at $Proxy90.invoke(Unknown Source)
  24.  
  25. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 49] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  26.     at sun.reflect.GeneratedMethodAccessor186.invoke(Unknown Source)
  27.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
  28.     at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
  29.     at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
  30.     at java.security.AccessController.doPrivileged1(Native Method)
  31.  
I want the Perl script to return everything from the file called "App.log" shown above, that matches the line containing "ID_UNHANDL ED", i.e. the line ...
Expand|Select|Wrap|Line Numbers
  1. "[ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator." 
  2. upto the next occurence of the string "[ERROR]" 
  3. i.e. it should include the following ...
  4.  
  5. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  6. ....
  7. ...
  8. ...
  9. nested exception is:
  10. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: 
  11. ...
  12. ...
  13. ...
  14. at $Proxy90.invoke(Unknown Source)
  15.  
which is the last line of that exception. It shouldn't include the next "[ERROR]" line.

I could come up with the following code but it returned only the lines containing the strings mentioned.

Expand|Select|Wrap|Line Numbers
  1. while ($line = <LOGFILE>) {
  2.      # chomp $line;
  3.     if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:.*$/) {
  4.          push @message, $line;
  5.          $match = 1;
  6.          next;
  7.         }
  8.     if ($match && ($line =~ /^nested exception is:/)) {
  9.         $line = <LOGFILE>;
  10.         push @message, $line;
  11.         $match = 0;
  12.         next;
  13.        }
  14.     }
  15.  
Apr 7 '09 #1
25 5950
KevinADC
4,059 Recognized Expert Specialist
its not clear if that is the entire log file or if there is more and you need to continue matching more records. If that is the entire log file then all you really need to do is use the "last" control to break the loop:

Expand|Select|Wrap|Line Numbers
  1. while ($line = <LOGFILE>) {
  2.    if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {
  3.       push @message, $line;
  4.    }
  5.    elsif ($line =~ /^nested exception is:/) {
  6.       $line = <LOGFILE>;
  7.       push @message, $line;
  8.       last;
  9.    }
  10. }
  11.  
Apr 7 '09 #2
joeferns79
37 New Member
That's not the entire log file. There are many more records like the one I showed that need to be matched. It's just that I want the entire stack to be printed and not just that 1 line.
Apr 8 '09 #3
Kelicula
176 Recognized Expert New Member
I was just skimming the post I may be mistaken but it seems you are using a non-greedy match. Were you want a greedy match.
Expand|Select|Wrap|Line Numbers
  1. if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {
  2.  
Take out the question mark after the * character.
Expand|Select|Wrap|Line Numbers
  1. if ($line =~ /^\[ERROR\](.*)infrastructure:ID_UNHANDLED:/) {
  2.  
Hope it helps!
Apr 10 '09 #4
joeferns79
37 New Member
No, it doesn't. It returns the same result ...

[ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure: ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
infrastructure: RUN_ID_RUNTIME: A runtime exception occurred: javax.transacti on.RollbackExce ption.
[ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 49] - infrastructure: ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
infrastructure: RUN_ID_RUNTIME: A runtime exception occurred: javax.transacti on.RollbackExce ption.

i.e. it returns just the line that the string matches and not the full stack.

I want it to return the following ...

[ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure: ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
at sun.reflect.Gen eratedMethodAcc essor186.invoke (Unknown Source)
at sun.reflect.Del egatingMethodAc cessorImpl.invo ke(DelegatingMe thodAccessorImp l.java(Compiled Code))
at java.lang.refle ct.Method.invok e(Method.java(C ompiled Code))
at com.ibm.rmi.uti l.ProxyUtil$2.r un(ProxyUtil.ja va(Compiled Code))
at java.security.A ccessController .doPrivileged1( Native Method)
at java.security.A ccessController .doPrivileged(A ccessController .java(Compiled Code))
at com.ibm.rmi.uti l.ProxyUtil.inv okeWithPrivileg e(ProxyUtil.jav a(Compiled Code))
at com.ibm.CORBA.i iop.ClientDeleg ate.invoke(Clie ntDelegate.java (Compiled Code))
at $Proxy90.invoke (Unknown Source)
nested exception is:
infrastructure: RUN_ID_RUNTIME: A runtime exception occurred: javax.transacti on.RollbackExce ption.
at sun.reflect.Gen eratedMethodAcc essor186.invoke (Unknown Source)
at sun.reflect.Del egatingMethodAc cessorImpl.invo ke(DelegatingMe thodAccessorImp l.java(Compiled Code))
at java.lang.refle ct.Method.invok e(Method.java(C ompiled Code))
at com.ibm.rmi.uti l.ProxyUtil$2.r un(ProxyUtil.ja va(Compiled Code))
at java.security.A ccessController .doPrivileged1( Native Method)
at java.security.A ccessController .doPrivileged(A ccessController .java(Compiled Code))
at com.ibm.rmi.uti l.ProxyUtil.inv okeWithPrivileg e(ProxyUtil.jav a(Compiled Code))
at com.ibm.CORBA.i iop.ClientDeleg ate.invoke(Clie ntDelegate.java (Compiled Code))
at $Proxy90.invoke (Unknown Source)

[ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 49] - infrastructure: ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
at sun.reflect.Gen eratedMethodAcc essor186.invoke (Unknown Source)
at sun.reflect.Del egatingMethodAc cessorImpl.invo ke(DelegatingMe thodAccessorImp l.java(Compiled Code))
at java.lang.refle ct.Method.invok e(Method.java(C ompiled Code))
at com.ibm.rmi.uti l.ProxyUtil$2.r un(ProxyUtil.ja va(Compiled Code))
at java.security.A ccessController .doPrivileged1( Native Method
Apr 10 '09 #5
Kelicula
176 Recognized Expert New Member
Hum....

I think your answer lies here?
Apr 10 '09 #6
joeferns79
37 New Member
I changed it to look like this ..
Expand|Select|Wrap|Line Numbers
  1. while ($line = <LOGFILE>) {
  2.      # chomp $line;
  3.     if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED: (.*)\[ERROR\]$/ms) {
  4.          push @message, $line;
  5.          $match = 1;
  6.          next;
  7.         }
  8.       }
  9.  
but now it won't return anything at all. Is the RE incorrect?
Apr 10 '09 #7
Kelicula
176 Recognized Expert New Member
Please use the code tags, so others can see it better and possibly use it to help you reach a solution. (copy&paste). Just an FYI.

I think one problem may be that "$line" is always filtered in "one line at a time" but you are attempting to capture more than one line at a time, but using push to append an array one line at a time. In other words...

No "single" line matches the criteria. You only check for match each line at a time, replacing the value of $line each iteration. Even if there was a match the multiple lines would have been pushed into message one at a time. Instead of what you want, each message element representing the whole match.

You will have to create a result string or something.
Like:

Expand|Select|Wrap|Line Numbers
  1.  
  2. my @message;
  3.  
  4. my $file = <LOGFILE>;
  5.  
  6. while($file =~ /^(\[ERROR\].*?infrastructure:ID_UNHANDLED: .*?)\[ERROR\]$/mgs) {
  7. push(@message, $1);
  8. }
  9.  
Untested though.

But the idea is:
Stream the file in continuously pushing the entire match into @message, one "match" at a time.

I removed the capturing parenthesis around the .'s, and added them around the actual match (without ending [ERROR]) So they would get captured in $1.

You may want to match the actual double \n\n's before the next [ERROR] so the next match is successful.

You DO still want the s and m modifiers. I added a g, for global matches.

I have no Perl on this computer, can't test anything...sorr y.
Apr 10 '09 #8
KevinADC
4,059 Recognized Expert Specialist
I'm still not clear on what it is you need to store in the array from the file.
Apr 10 '09 #9
joeferns79
37 New Member
Kelicula,

I changed it as per your suggestions, but it won't return anything at all...

Expand|Select|Wrap|Line Numbers
  1.   sub genreport {
  2.  
  3.    my ($logfile, $reportfile) = @_;
  4.    my ($file, @message);
  5.  
  6.    open(LOGFILE, "<$logfile")
  7.       or die "Can't open $logfile: $!";
  8.  
  9.    open (REPORTFILE, ">$reportfile")
  10.       or die "Can't open $reportfile: $!";
  11.  
  12.  
  13.    while($file = <LOGFILE>) {
  14.      # chomp $line;
  15.     if ($file =~ /^(\[ERROR\].*?infrastructure:ID_UNHANDLED: .*?)\[ERROR\]$/mgs) {
  16.          push @message, $1;
  17.  
  18.         }
  19.  
  20.     }
  21. }
  22.  
  23.  

KevinADC - I want to capture the following contents from the log ...

Expand|Select|Wrap|Line Numbers
  1. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator. 
  2.     at sun.reflect.GeneratedMethodAccessor186.invoke(Unknown Source) 
  3.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code)) 
  4.     at java.lang.reflect.Method.invoke(Method.java(Compiled Code)) 
  5.     at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code)) 
  6.     at java.security.AccessController.doPrivileged1(Native Method) 
  7.     at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code)) 
  8.     at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code)) 
  9.     at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.java(Compiled Code)) 
  10.     at $Proxy90.invoke(Unknown Source) 
  11. nested exception is: 
  12. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException. 
  13.         at sun.reflect.GeneratedMethodAccessor186.invoke(Unknown Source) 
  14.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code)) 
  15.     at java.lang.reflect.Method.invoke(Method.java(Compiled Code)) 
  16.     at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code)) 
  17.     at java.security.AccessController.doPrivileged1(Native Method) 
  18.     at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code)) 
  19.     at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code)) 
  20.     at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.java(Compiled Code)) 
  21.     at $Proxy90.invoke(Unknown Source) 
  22.  
The above belongs to 1 un-handled exception. But the log file contains many un-handled exceptions, so I want all these returned.
Apr 14 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

20
5767
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT" >>>c = extract(a,b) >>>print c "abcdefghijklmnop"
0
1935
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match should be case-insensitive, but extract should have case preserved.
2
2136
by: cricfan | last post by:
I'm parsing a text file to extract word definitions. For example the input text file contains the following content: di.va.gate \'di_--v*-.ga_-t\ vb pas.sim \'pas-*m\ adv : here and there : THROUGHOUT I am trying to obtain words between two literal backslashes (\ .. \). I am not able to match words between two literal backslashes using the regxp - re.compile(r'\\*\\').
11
11817
by: elrondrules | last post by:
Hi Am pretty new to python and hence this question.. I have file with an output of a process. I need to search this file one line at a time and my pattern is that I am looking for the lines that has the word 'event' and the word 'new'. Note that i need lines that has both the words only and not either one of them..
17
2892
by: Umesh | last post by:
Can anyone do it? ARMY1987- what say?
6
4496
by: pramodkh | last post by:
Hi All Today only i joined this group. need ur help. Here goes my question: Is there any simple way to match for a particular pattern ( with start and end delimitters) and extract the matched section? I am doing it reading the contents line by line and setting the flags. But this logic is becoming complex, as i need to match and extract based on certain conditions. for ex:
1
5061
by: manishabh77 | last post by:
I will be obliged if anybody can help me with this problem: I am trying to extract data from an excel sheet that matches IDs given in column 4 of the excel sheet.I have stored those query IDs in an array (@names). After I look for the match in this section of the code: if ($value=~/^$names$/), I want to write out only those rows that satisfy the above natch condition. But currently the code I have here writes out everything. How do I get it to...
1
2723
by: Edwin.Madari | last post by:
from each line separate out url and request parts. split the request into key-value pairs, use urllib to unquote key-value pairs......as show below... import urllib line = "GET...
3
5033
by: dmalhotr2001 | last post by:
Hi, For string extraction function in vb, if I feed in a paragraph, how do I extract the first sentence of that paragraph. Thanks :D
0
8694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8621
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8928
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8890
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3060
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.