Match and Extract everything between 2 words in a file | Member | | Join Date: Sep 2008
Posts: 35
| |
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... -
[ERROR] [06 Apr 2009 09:45:56] [Trace] [WebContainer : 48] - App Number: 0
-
[ERROR] [06 Apr 2009 09:45:56] [Trace] [WebContainer : 48] - Response for AE Completion No such RID Found
-
[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(Unknown Source)
-
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
-
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
-
at java.security.AccessController.doPrivileged1(Native Method)
-
at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code))
-
at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.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(Unknown Source)
-
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
-
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
-
at java.security.AccessController.doPrivileged1(Native Method)
-
at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code))
-
at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.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(Unknown Source)
-
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
-
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
-
at java.security.AccessController.doPrivileged1(Native Method)
-
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 ... -
"[ERROR] [06 Apr 2009 09:46:02] [Trace] [WebContainer : 20] - infrastructure:ID_UNHANDLED: An un-handled server exception occurred. Please contact your administrator."
-
upto the next occurence of the string "[ERROR]"
-
i.e. it should include 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.
-
....
-
...
-
...
-
nested exception is:
-
infrastructure:RUN_ID_RUNTIME: A runtime exception occurred:
-
...
-
...
-
...
-
at $Proxy90.invoke(Unknown Source)
-
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. -
while ($line = <LOGFILE>) {
-
# chomp $line;
-
if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:.*$/) {
-
push @message, $line;
-
$match = 1;
-
next;
-
}
-
if ($match && ($line =~ /^nested exception is:/)) {
-
$line = <LOGFILE>;
-
push @message, $line;
-
$match = 0;
-
next;
-
}
-
}
-
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | 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: -
while ($line = <LOGFILE>) {
-
if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {
-
push @message, $line;
-
}
-
elsif ($line =~ /^nested exception is:/) {
-
$line = <LOGFILE>;
-
push @message, $line;
-
last;
-
}
-
}
-
| | Member | | Join Date: Sep 2008
Posts: 35
| | | 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.
|  | Expert | | Join Date: Jul 2007
Posts: 169
| | | 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. -
if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {
-
Take out the question mark after the * character. -
if ($line =~ /^\[ERROR\](.*)infrastructure:ID_UNHANDLED:/) {
-
Hope it helps!
| | Member | | Join Date: Sep 2008
Posts: 35
| | | 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
|  | Expert | | Join Date: Jul 2007
Posts: 169
| | | re: Match and Extract everything between 2 words in a file | | Member | | Join Date: Sep 2008
Posts: 35
| | | re: Match and Extract everything between 2 words in a file
I changed it to look like this .. -
while ($line = <LOGFILE>) {
-
# chomp $line;
-
if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED: (.*)\[ERROR\]$/ms) {
-
push @message, $line;
-
$match = 1;
-
next;
-
}
-
}
-
but now it won't return anything at all. Is the RE incorrect?
|  | Expert | | Join Date: Jul 2007
Posts: 169
| | | 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: -
-
my @message;
-
-
my $file = <LOGFILE>;
-
-
while($file =~ /^(\[ERROR\].*?infrastructure:ID_UNHANDLED: .*?)\[ERROR\]$/mgs) {
-
push(@message, $1);
-
}
-
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.
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | 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
| | | 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... -
sub genreport {
-
-
my ($logfile, $reportfile) = @_;
-
my ($file, @message);
-
-
open(LOGFILE, "<$logfile")
-
or die "Can't open $logfile: $!";
-
-
open (REPORTFILE, ">$reportfile")
-
or die "Can't open $reportfile: $!";
-
-
-
while($file = <LOGFILE>) {
-
# chomp $line;
-
if ($file =~ /^(\[ERROR\].*?infrastructure:ID_UNHANDLED: .*?)\[ERROR\]$/mgs) {
-
push @message, $1;
-
-
}
-
-
}
-
}
-
-
KevinADC - I want to capture the following contents from the log ... -
[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(Unknown Source)
-
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
-
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
-
at java.security.AccessController.doPrivileged1(Native Method)
-
at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code))
-
at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.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(Unknown Source)
-
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
-
at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil$2.run(ProxyUtil.java(Compiled Code))
-
at java.security.AccessController.doPrivileged1(Native Method)
-
at java.security.AccessController.doPrivileged(AccessController.java(Compiled Code))
-
at com.ibm.rmi.util.ProxyUtil.invokeWithPrivilege(ProxyUtil.java(Compiled Code))
-
at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.java(Compiled Code))
-
at $Proxy90.invoke(Unknown Source)
-
The above belongs to 1 un-handled exception. But the log file contains many un-handled exceptions, so I want all these returned.
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Match and Extract everything between 2 words in a file
try this: -
OUTTER: while ($line = <LOGFILE>) {
-
if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {
-
push @message, $line;
-
while ($line = <LOGFILE>) {
-
redo OUTTER if ($line =~ /^\[ERROR\]/);
-
push @message, $line;
-
}
-
}
-
}
-
|  | Expert | | Join Date: Jul 2007
Posts: 169
| | | re: Match and Extract everything between 2 words in a file Quote:
Originally Posted by joeferns79 Kelicula,
I changed it as per your suggestions, but it won't return anything at all... -
sub genreport {
-
-
my ($logfile, $reportfile) = @_;
-
my ($file, @message);
-
-
open(LOGFILE, "<$logfile")
-
or die "Can't open $logfile: $!";
-
-
open (REPORTFILE, ">$reportfile")
-
or die "Can't open $reportfile: $!";
-
-
-
while($file = <LOGFILE>) {
-
# chomp $line;
-
if ($file =~ /^(\[ERROR\].*?infrastructure:ID_UNHANDLED: .*?)\[ERROR\]$/mgs) {
-
push @message, $1;
-
-
}
-
-
}
-
}
-
-
The main thing to notice in my suggestion was this line:
Slurping the whole file into a single var, and then saying... -
while(match){
-
push $message, $1;
-
}
-
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)
This prints to my terminal: -
[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 : 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 : 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)
-
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 |  | Expert | | Join Date: Jul 2007
Posts: 169
| | | 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: -
while($file =~ /^(\[ERROR\].*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
-
push(@message, $1);
-
}
-
|  | Expert | | Join Date: Jul 2007
Posts: 169
| | | re: Match and Extract everything between 2 words in a file
That didn't work. :(
But this did. -
while($file =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
-
push(@message, $1);
-
}
-
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
| | | 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 ... -
use strict;
-
use warnings;
-
-
-
####################################################################
-
# genreport
-
####################################################################
-
sub genreport {
-
-
my ($logfile, @message);
-
-
open(LOGFILE, "<$logfile")
-
or die "Can't open $logfile: $!";
-
-
my $file = do { local $/; <LOGFILE> };
-
-
while($file =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
-
push(@message, $1);
-
}
-
-
print "@message";
-
-
}
-
I even changed the RE to the following and still nothing... -
use strict;
-
use warnings;
-
-
-
####################################################################
-
# genreport
-
####################################################################
-
sub genreport {
-
-
my ($logfile, @message);
-
-
open(LOGFILE, "<$logfile")
-
or die "Can't open $logfile: $!";
-
-
my $file = do { local $/; <LOGFILE> };
-
-
while($file =~ /^(\[ERROR\].*?)\n\n/mgs){
-
push(@message, $1);
-
}
-
-
print "@message";
-
-
}
-
-
| | Member | | Join Date: Sep 2008
Posts: 35
| | | re: Match and Extract everything between 2 words in a file
KevinADC,
I tried your suggestion too, and that didn't return anything either ... -
-
use strict;
-
use warnings;
-
-
-
####################################################################
-
# genreport
-
####################################################################
-
sub genreport {
-
-
my ($logfile, $line, @message);
-
-
open(LOGFILE, "<$logfile")
-
or die "Can't open $logfile: $!";
-
-
-
OUTTER: while ($line = <LOGFILE>) {
-
if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {
-
push @message, $line;
-
while ($line = <LOGFILE>) {
-
redo OUTTER if ($line =~ /^\[ERROR\]/);
-
push @message, $line;
-
}
-
}
-
}
-
-
print "@message";
-
-
}
-
-
|  | Expert | | Join Date: Jul 2007
Posts: 169
| | | 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
| | | 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.
|  | Expert | | Join Date: Sep 2008 Location: Sydney, Australia
Posts: 173
| | | 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
| | | re: Match and Extract everything between 2 words in a file
Here's the batch file I call... -
@echo off
-
-
set PERL_HOME=C:\Perl
-
set PATH=%PERL_HOME%\bin;%PATH%
-
-
set script=UAESummary.bat
-
-
-
-
perl UAESummary2.pl %1
-
-
if errorlevel 1 (
-
echo.
-
echo %script%: download failed
-
exit /b 1
-
)
-
|  | Expert | | Join Date: Sep 2008 Location: Sydney, Australia
Posts: 173
| | | re: Match and Extract everything between 2 words in a file
I would put the path to the log file under -
my ($logfile, $line, @message);
-
add -
$logfile='PATH\TO\Report.log';
-
| | Member | | Join Date: Sep 2008
Posts: 35
| | | re: Match and Extract everything between 2 words in a file
No help there ... -
use strict;
-
use warnings;
-
-
-
####################################################################
-
# genreport
-
####################################################################
-
sub genreport {
-
-
my ($logfile, $line, @message);
-
-
$logfile='C:\PerlScripts\CuramApp.log';
-
-
-
-
OUTTER: while ($line = $logfile) {
-
if ($line =~ /^\[ERROR\](.*?)infrastructure:ID_UNHANDLED:/) {
-
push @message, $line;
-
while ($line = $logfile) {
-
redo OUTTER if ($line =~ /^\[ERROR\]/);
-
push @message, $line;
-
}
-
}
-
}
-
-
print "@message";
-
-
}
-
Also, tried ... -
use strict;
-
use warnings;
-
-
-
####################################################################
-
# genreport
-
####################################################################
-
sub genreport {
-
-
my ($logfile, @message);
-
-
$logfile='C:\PerlScripts\CuramApp.log';
-
-
my $file = do { local $/; $logfile };
-
-
while($file =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
-
push(@message, $1);
-
}
-
-
print "@message";
-
-
}
-
-
|  | Expert | | Join Date: Sep 2008 Location: Sydney, Australia
Posts: 173
| | | re: Match and Extract everything between 2 words in a file
are you ever calling your Sub ? ever ?
Add this above sub genreport { | | Member | | Join Date: Sep 2008
Posts: 35
| | | 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... -
use strict;
-
-
-
-
####################################################################
-
# genreport
-
####################################################################
-
sub genreport {
-
my ($logfile, $reportfile) = @_;
-
my ($line, @message, @summary, $count, $match, $n);
-
-
open(LOGFILE, "<$logfile")
-
or die "Can't open $logfile: $!";
-
-
open (REPORTFILE, ">$reportfile")
-
or die "Can't open $reportfile: $!";
-
-
-
$count = $match = 0;
-
while ($line = <LOGFILE>) {
-
# chomp $line;
-
if ($line =~ /^(\[ERROR\](?-s:.*)infrastructure:ID_UNHANDLED.*?infrastructure:RUN_ID_RUNTIME.*?)\n\n/mgs) {
-
push @message, $line;
-
$match = 1;
-
next;
-
}
-
-
}
-
for (@message) {
-
print;
-
}
-
-
}
-
-
####################################################################
-
# main
-
####################################################################
-
-
if (@ARGV != 2) {
-
die "usage: UAESummary.pl <logfile> <reportfile>";
-
}
-
-
my ($logfile, $reportfile) = @ARGV;
-
-
genreport($logfile, $reportfile);
-
-
exit 0;
-
-
|  | Expert | | Join Date: Sep 2008 Location: Sydney, Australia
Posts: 173
| | | 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
| | | 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.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|