Connecting Tech Pros Worldwide Help | Site Map

Match and Extract everything between 2 words in a file

Member
 
Join Date: Sep 2008
Posts: 35
#1: Apr 7 '09
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_UNHANDLED", 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.  
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Apr 7 '09

re: Match and Extract everything between 2 words in a file


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.  
Member
 
Join Date: Sep 2008
Posts: 35
#3: Apr 8 '09

re: Match and Extract everything between 2 words in a file


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.
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#4: Apr 10 '09

re: Match and Extract everything between 2 words in a file


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!
Member
 
Join Date: Sep 2008
Posts: 35
#5: Apr 10 '09

re: Match and Extract everything between 2 words in a file


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.transaction.RollbackException.
[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.transaction.RollbackException.

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.GeneratedMethodAccessor186.invoke(Unkn own Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
at java.security.AccessController.doPrivileged1(Nativ e Method)
at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
at $Proxy90.invoke(Unknown Source)
nested exception is:
infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
at java.security.AccessController.doPrivileged1(Nativ e Method)
at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.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.GeneratedMethodAccessor186.invoke(Unkn own Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
at java.security.AccessController.doPrivileged1(Nativ e Method
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#6: Apr 10 '09

re: Match and Extract everything between 2 words in a file


Hum....

I think your answer lies here?
Member
 
Join Date: Sep 2008
Posts: 35
#7: Apr 10 '09

re: Match and Extract everything between 2 words in a file


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?
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#8: Apr 10 '09

re: Match and Extract everything between 2 words in a file


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...sorry.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#9: Apr 10 '09

re: Match and Extract everything between 2 words in a file


I'm still not clear on what it is you need to store in the array from the file.
Member
 
Join Date: Sep 2008
Posts: 35
#10: Apr 14 '09

re: Match and Extract everything between 2 words in a file


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.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#11: Apr 14 '09

re: Match and Extract everything between 2 words in a file


try this:

Expand|Select|Wrap|Line Numbers
  1. OUTTER: while ($line = <LOGFILE>) {
  2.    if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {
  3.       push @message, $line;
  4.       while ($line = <LOGFILE>) {
  5.          redo OUTTER if ($line =~ /^\[ERROR\]/);
  6.          push @message, $line;
  7.       }
  8.    }
  9. }
  10.  
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#12: Apr 14 '09

re: Match and Extract everything between 2 words in a file


Quote:

Originally Posted by joeferns79 View Post

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.  


The main thing to notice in my suggestion was this line:
Expand|Select|Wrap|Line Numbers
  1. my $file = <LOGFILE>;
  2.  
Slurping the whole file into a single var, and then saying...

Expand|Select|Wrap|Line Numbers
  1. while(match){
  2. push $message, $1;
  3. }
  4.  
In your genreport sub you are still testing for a match "one line at a time".

Using the s modifier allows perl's dot character to match all characters INCLUDING a newline. (which is usually the only thing excluded)

And the m modifier changes ^ and $ to represent the beginning and ending of ANY newline, not necessarily the next one.

I noticed in the LOGFILE the chunks were separated by two newlines. THAT would be a good terminator.

I must admit I did forget about the "holdTerminator". $/
You must undefine that to properly slurp the file.
Slurping


So...
(tested on my computer)

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my @message;
  5.  
  6. my $file = do { local $/; <DATA> }; # slurp file, undefined $/ only for this scope.
  7.  
  8. # while there is a match, populate message array with match.
  9. while($file =~ /^(\[ERROR\].*?)\n\n/mgs) {
  10. push(@message, $1);
  11.  
  12. }
  13.  
  14. $" = "\n\n"; # Added this just so I could see matches in my terminal instead of one long string.
  15.  
  16. print "@message";
  17.  
  18.  
  19. __DATA__
  20.  
  21.  
  22. kllbjbjebe ejhbbhkebef ionelf enjj kf mvg
  23. e,m eikb e e3ugbgbfbjhefb f
  24. ekeibejb fh eih efvfbehfuefi b kjfjnhevev
  25. khefbe fikebffhebfhnemjfjdijkfj knj3vb3hyv fjbedvf
  26. efjiefhne fjbefne envfmelfdihvbe ejfgv efkjlv
  27.  
  28.  
  29. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  30. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  31. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  32. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  33. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  34. at java.security.AccessController.doPrivileged1(Nativ e Method)
  35. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  36. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  37. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  38. at $Proxy90.invoke(Unknown Source)
  39. nested exception is:
  40. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
  41. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  42. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  43. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  44. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  45. at java.security.AccessController.doPrivileged1(Nativ e Method)
  46. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  47. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  48. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  49. at $Proxy90.invoke(Unknown Source)
  50.  
  51.  
  52. kllbjbjebe ejhbbhkebef ionelf enjj kf mvg
  53. e,m eikb e e3ugbgbfbjhefb f
  54. ekeibejb fh eih efvfbehfuefi b kjfjnhevev
  55. khefbe fikebffhebfhnemjfjdijkfj knj3vb3hyv fjbedvf
  56. efjiefhne fjbefne envfmelfdihvbe ejfgv efkjlv
  57.  
  58. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  59. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  60. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  61. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  62. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  63. at java.security.AccessController.doPrivileged1(Nativ e Method)
  64. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  65. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  66. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  67. at $Proxy90.invoke(Unknown Source)
  68. nested exception is:
  69. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
  70. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  71. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  72. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  73. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  74. at java.security.AccessController.doPrivileged1(Nativ e Method)
  75. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  76. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  77. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  78. at $Proxy90.invoke(Unknown Source)
  79.  
  80. kllbjbjebe ejhbbhkebef ionelf enjj kf mvg
  81. e,m eikb e e3ugbgbfbjhefb f
  82. ekeibejb fh eih efvfbehfuefi b kjfjnhevev
  83. khefbe fikebffhebfhnemjfjdijkfj knj3vb3hyv fjbedvf
  84. efjiefhne fjbefne envfmelfdihvbe ejfgv efkjlv
  85.  
  86. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  87. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  88. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  89. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  90. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  91. at java.security.AccessController.doPrivileged1(Nativ e Method)
  92. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  93. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  94. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  95. at $Proxy90.invoke(Unknown Source)
  96. nested exception is:
  97. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
  98. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  99. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  100. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  101. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  102. at java.security.AccessController.doPrivileged1(Nativ e Method)
  103. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  104. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  105. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  106. at $Proxy90.invoke(Unknown Source)
  107.  
This prints to my terminal:
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(Unkn own Source)
  3. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  4. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  5. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  6. at java.security.AccessController.doPrivileged1(Nativ e Method)
  7. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  8. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  9. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.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(Unkn own Source)
  14. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  15. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  16. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  17. at java.security.AccessController.doPrivileged1(Nativ e Method)
  18. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  19. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  20. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  21. at $Proxy90.invoke(Unknown Source)
  22.  
  23. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  24. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  25. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  26. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  27. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  28. at java.security.AccessController.doPrivileged1(Nativ e Method)
  29. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  30. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  31. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  32. at $Proxy90.invoke(Unknown Source)
  33. nested exception is:
  34. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
  35. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  36. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  37. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  38. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  39. at java.security.AccessController.doPrivileged1(Nativ e Method)
  40. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  41. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  42. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  43. at $Proxy90.invoke(Unknown Source)
  44.  
  45. [ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator.
  46. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  47. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  48. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  49. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  50. at java.security.AccessController.doPrivileged1(Nativ e Method)
  51. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  52. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  53. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  54. at $Proxy90.invoke(Unknown Source)
  55. nested exception is:
  56. infrastructure:RUN_ID_RUNTIME: A runtime exception occurred: javax.transaction.RollbackException.
  57. at sun.reflect.GeneratedMethodAccessor186.invoke(Unkn own Source)
  58. at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java(Compiled Code))
  59. at java.lang.reflect.Method.invoke(Method.java(Compil ed Code))
  60. at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Co mpiled Code))
  61. at java.security.AccessController.doPrivileged1(Nativ e Method)
  62. at java.security.AccessController.doPrivileged(Access Controller.java(Compiled Code))
  63. at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(Pro xyUtil.java(Compiled Code))
  64. at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDel egate.java(Compiled Code))
  65. at $Proxy90.invoke(Unknown Source)
  66.  
Just change the DATA to your file handle. "LOGFILE".

One word of caution.
When you slurp, don't do anything else with the data. It takes about as long as assembling an array itself. (If not slightly longer) AND if you then copy the slurped file into an array, you'll have TWO copies on disk.

Using it for this should not be a problem, but just be mindful.
Feel free to Benchmark!
It's your friend.


PS- Code tags use square brackets not angle. :)

PPS- What do ya know? There's a mod on CPAN for this!!
File::Slurp
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#13: Apr 14 '09

re: Match and Extract everything between 2 words in a file


I noticed some lines say [ERROR] but you don't want them.
Just add the "infrastructure:RUN_ID_RUNTIME" back into the regexe.

This should do it:
Expand|Select|Wrap|Line Numbers
  1. while($file =~ /^(\[ERROR\].*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
  2. push(@message, $1);
  3. }
  4.  
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#14: Apr 15 '09

re: Match and Extract everything between 2 words in a file


That didn't work. :(

But this did.

Expand|Select|Wrap|Line Numbers
  1. while($file =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
  2. push(@message, $1);
  3. }
  4.  
Assuming you want only lines that start with [ERROR] followed by any character EXCEPT a newline, followed by "infrastructure:ID_UNHANDLED", followed by any character INCLUDING a newline, followed by "infrastructure:RUN_ID_RUNTIME", followed by any character INCLUDING newlines terminating on 2 newlines.

WHEEEWW!!

OK. That successfully filtered these lines from my test:

[ERROR] Undefined

[ERROR] jnjbg

But MATCHED the ones (I think) you want.

Let me know how it comes out!
Member
 
Join Date: Sep 2008
Posts: 35
#15: Apr 20 '09

re: Match and Extract everything between 2 words in a file


I have no idea what's wrong with my script but even after making it look exactly like you mentioned, it doesn't output anything ...

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4.  
  5. ####################################################################
  6. # genreport
  7. ####################################################################
  8.   sub genreport {
  9.  
  10.    my ($logfile, @message); 
  11.  
  12.    open(LOGFILE, "<$logfile")
  13.       or die "Can't open $logfile: $!";
  14.  
  15.    my $file = do { local $/; <LOGFILE> };
  16.  
  17.    while($file =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) { 
  18. push(@message, $1); 
  19.  
  20. print "@message";
  21.  
  22. }
  23.  
I even changed the RE to the following and still nothing...

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4.  
  5. ####################################################################
  6. # genreport
  7. ####################################################################
  8.   sub genreport {
  9.  
  10.    my ($logfile, @message); 
  11.  
  12.    open(LOGFILE, "<$logfile")
  13.       or die "Can't open $logfile: $!";
  14.  
  15.    my $file = do { local $/; <LOGFILE> };
  16.  
  17.    while($file =~ /^(\[ERROR\].*?)\n\n/mgs){
  18.     push(@message, $1); 
  19.   } 
  20.  
  21.   print "@message";
  22.  
  23. }
  24.  
  25.  
Member
 
Join Date: Sep 2008
Posts: 35
#16: Apr 20 '09

re: Match and Extract everything between 2 words in a file


KevinADC,
I tried your suggestion too, and that didn't return anything either ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3. use warnings;
  4.  
  5.  
  6. ####################################################################
  7. # genreport
  8. ####################################################################
  9.   sub genreport {
  10.  
  11.    my ($logfile, $line, @message); 
  12.  
  13.    open(LOGFILE, "<$logfile")
  14.       or die "Can't open $logfile: $!";
  15.  
  16.  
  17. OUTTER: while ($line = <LOGFILE>) { 
  18.          if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) { 
  19.            push @message, $line; 
  20.          while ($line = <LOGFILE>) { 
  21.           redo OUTTER if ($line =~ /^\[ERROR\]/); 
  22.          push @message, $line; 
  23.       } 
  24.    } 
  25.  
  26. print "@message";
  27.  
  28. }
  29.  
  30.  
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#17: Apr 20 '09

re: Match and Extract everything between 2 words in a file


Hummm....
How are you calling on genreport?
What exactly do you pass to it?
Is it in the current package, or a separate file?

Very strange.

Have you tried KevinADC's code?
It looks like it works too. (using too objectively..:)

I swear my code worked in Komodo with Perl 5.10 on Windows.

Of course I didn't have the exact same logfile to scan.
Member
 
Join Date: Sep 2008
Posts: 35
#18: Apr 20 '09

re: Match and Extract everything between 2 words in a file


I am calling it through a batch file in a command prompt in Windows and pass the name of the log file, i.e.

UAESummary.bat AppLog.txt > Report.log


Yes, I tried KevinADC's code too, to no avail.
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#19: Apr 21 '09

re: Match and Extract everything between 2 words in a file


he means how are you running the Perl script
if the script is being run by that Batch file, may you post the code to that batch file here.
in [CODE*] [/CODE*] Tags (take out the * for code tags to work)
Member
 
Join Date: Sep 2008
Posts: 35
#20: Apr 21 '09

re: Match and Extract everything between 2 words in a file


Here's the batch file I call...
Expand|Select|Wrap|Line Numbers
  1. @echo off
  2.  
  3. set PERL_HOME=C:\Perl
  4. set PATH=%PERL_HOME%\bin;%PATH%
  5.  
  6. set script=UAESummary.bat
  7.  
  8.  
  9.  
  10. perl UAESummary2.pl %1
  11.  
  12. if errorlevel 1 (
  13.    echo.
  14.    echo %script%: download failed
  15.    exit /b 1
  16. )
  17.  
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#21: Apr 22 '09

re: Match and Extract everything between 2 words in a file


I would put the path to the log file under
Expand|Select|Wrap|Line Numbers
  1. my ($logfile, $line, @message);
  2.  
add
Expand|Select|Wrap|Line Numbers
  1. $logfile='PATH\TO\Report.log';
  2.  
Member
 
Join Date: Sep 2008
Posts: 35
#22: Apr 22 '09

re: Match and Extract everything between 2 words in a file


No help there ...

Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3.  
  4.  
  5. #################################################################### 
  6. # genreport 
  7. #################################################################### 
  8.   sub genreport { 
  9.  
  10.    my ($logfile, $line, @message);  
  11.  
  12.    $logfile='C:\PerlScripts\CuramApp.log';
  13.  
  14.  
  15.  
  16. OUTTER: while ($line = $logfile) {  
  17.          if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {  
  18.            push @message, $line;  
  19.          while ($line = $logfile) {  
  20.           redo OUTTER if ($line =~ /^\[ERROR\]/);  
  21.          push @message, $line;  
  22.       }  
  23.    }  
  24. }  
  25.  
  26. print "@message"; 
  27.  
  28.  
Also, tried ...

Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3.  
  4.  
  5. #################################################################### 
  6. # genreport 
  7. #################################################################### 
  8.   sub genreport { 
  9.  
  10.    my ($logfile, @message);  
  11.  
  12.    $logfile='C:\PerlScripts\CuramApp.log';
  13.  
  14.    my $file = do { local $/; $logfile }; 
  15.  
  16.    while($file =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {  
  17. push(@message, $1);  
  18. }  
  19.  
  20. print "@message"; 
  21.  
  22.  
  23.  
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#23: Apr 23 '09

re: Match and Extract everything between 2 words in a file


are you ever calling your Sub ? ever ?


Add this above sub genreport {
Expand|Select|Wrap|Line Numbers
  1. &genreport;
Member
 
Join Date: Sep 2008
Posts: 35
#24: May 4 '09

re: Match and Extract everything between 2 words in a file


Sorry for the delay in replying but yes, I am. I am still not seeing any output...

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3.  
  4.  
  5. ####################################################################
  6. # genreport
  7. ####################################################################
  8.   sub genreport {
  9.    my ($logfile, $reportfile) = @_;
  10.    my ($line, @message, @summary, $count, $match, $n);
  11.  
  12.    open(LOGFILE, "<$logfile")
  13.       or die "Can't open $logfile: $!";
  14.  
  15.    open (REPORTFILE, ">$reportfile")
  16.       or die "Can't open $reportfile: $!";
  17.  
  18.  
  19.    $count = $match = 0;
  20.    while ($line = <LOGFILE>) {
  21.      # chomp $line;
  22.     if ($line =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
  23.          push @message, $line;
  24.          $match = 1;
  25.          next;
  26.         }
  27.  
  28.     }
  29.    for (@message) {
  30.    print;
  31.  }
  32.  
  33. }
  34.  
  35. ####################################################################
  36. # main
  37. ####################################################################
  38.  
  39. if (@ARGV != 2) {
  40.    die "usage: UAESummary.pl <logfile> <reportfile>";
  41. }
  42.  
  43. my ($logfile, $reportfile) = @ARGV;
  44.  
  45. genreport($logfile, $reportfile);
  46.  
  47. exit 0;
  48.  
  49.  
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#25: May 15 '09

re: Match and Extract everything between 2 words in a file


sorry for the delay on my end i have been testing this for weeks now, and just found the answer but first i need to know how your logs are set out is it 1 big single line or is it a multi-lined?
past a log file here as a file attachment
Member
 
Join Date: Sep 2008
Posts: 35
#26: Jul 22 '09

re: Match and Extract everything between 2 words in a file


Icecrack, I was going through the forum and I noticed that you had posted a reply. I know it's like 2 months since you replied and I am not sure if you'll get this, but yes, it's a multi-line log.
Reply