0

我是 perl 新手,我们有一个类似于以下的日志文件:

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.

现在我需要一个脚本来只显示有 ORA-XXX 错误的脚本路径,脚本应该只显示与 ORA-xxx 错误相关的 SQL> @D:\Luntbuild_Testing\ 的路径,我在下面尝试过,你能帮帮我吗加强同样的。

$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); 
4

2 回答 2

1

当您看到> @标记时,不要立即打印该行,而是将其存储在一个变量中,并且仅在您实际看到错误时才将其打印出来:

$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);

另外:最好在脚本的顶部 编写use strict;和。强制您使用 显式命名局部变量,这会由于拼写错误而捕获很多错误。use warnings;use strict;my

于 2012-08-09T11:29:51.963 回答
1

我会做一些与你尝试过的非常相似的事情:

$file = 'c:\data.txt';
open(F, $file);
my $last_cmd = '';
while (<F>) {
  $last_cmd = $_ if /^SQL\> \@D:/;
  print $last_cmd if /^ORA-/;
}
于 2012-08-09T11:31:49.823 回答