user1587062
user1587062

Reputation: 5

Perl - customize log files for displaying only specific errors

I am new to perl, we have a log file similar to below:

SQL> @D:\Luntbuild_Testing\ASCMPK\Files\MAIN\DATABASE\HOST\FILES\DDL\20120412_152632__1_CLTM_EVENT_ACC_ROLE_BLOCK.DDL
SQL> CREATE TABLE CLTM_EVENT_ACC_ROLE_BLOCK
  2  (
  3  EVENT_CODE  VARCHAR2(4) ,
  4  ACC_ROLE  VARCHAR2(20)
  5  )
  6  ;
CREATE TABLE CLTM_EVENT_ACC_ROLE_BLOCK
             *
ERROR at line 1:
ORA-00955: name is already used by an existing object 


SQL> @D:\Luntbuild_Testing\ASCMPK\Files\MAIN\DATABASE\HOST\FILES\DDL\20120412_173845__2_CLTM_EVENT_ACC_ROLE_BLOCK.DDL
SQL> DROP TABLE  CLTM_EVENT_ACC_ROLE_BLOCK;

Table dropped.

Now I need a script to display only the script paths that have ORA-XXX errors, script should display only the path of the SQL> @D:\Luntbuild_Testing\ associated with ORA-xxx errors, I have tried below can you please help me to enhance the same.

$file = 'c:\data.txt';
open(txt, $file);
while($line = <txt>) {
print "$line" if $line =~ /> @/; #here i want the output to display the path of the script with only ORA-xxx errors and ignore if there are no errors
print "$line" if $line =~ /ORA-/;
}
close(txt); 

Upvotes: 0

Views: 131

Answers (2)

Jon
Jon

Reputation: 188

I would do something pretty similar to what you tried:

$file = 'c:\data.txt';
open(F, $file);
my $last_cmd = '';
while (<F>) {
  $last_cmd = $_ if /^SQL\> \@D:/;
  print $last_cmd if /^ORA-/;
}

Upvotes: 1

j_random_hacker
j_random_hacker

Reputation: 51226

Instead of immediately printing the line when you see the > @ marker, store it in a variable, and only print it out if and when you actually see an error:

$file = 'c:\data.txt';
open(txt, $file);
while($line = <txt>) {
$fn = $line if $line =~ /> @/; #here i want the output to display the path of the script with only ORA-xxx errors and ignore if there are no errors
print $fn, $line if $line =~ /ORA-/;
}
close(txt);

Also: it's good practice to write use strict; and use warnings; at the top of your script. use strict; forces you to explicitly name your local variables with my, which catches a lot of errors due to misspellings.

Upvotes: 1

Related Questions