2012-05-08 2 views
3

나는 Oracle 파일을 정리하는 작업을하고 있으며, 오라클 스키마 이름이 파일 내의 함수/프로 시저/패키지 이름 앞에 붙는 파일에서 문자열을 교체해야 할 때뿐만 아니라 함수/프로 시저/패키지 이름은 큰 따옴표로 묶입니다. 정의가 수정되면 실제 코드의 나머지 부분과 함께 파일에 수정 내용을 다시 씁니다.앵커 단어 사이에서 문자열을 캡처하는 Perl 정규식

간단한 선언 (입력/출력 매개 변수 없음)을 대체하기 위해 작성된 코드가 있습니다. 이제 작동 할 정규식을 얻으려고합니다. (참고 :이 포스트는 this question에서 계속됩니다.) 정리 :

교체 :

CREATE OR REPLACE FUNCTION "TRON2000"."DC_F_DUMP_CSV_MMA" (
p_trailing_separator IN BOOLEAN DEFAULT FALSE, 
p_max_linesize IN NUMBER DEFAULT 32000, 
p_mode IN VARCHAR2 DEFAULT 'w' 
) 
RETURN NUMBER 
IS 

CREATE OR REPLACE FUNCTION DC_F_DUMP_CSV_MMA (
p_trailing_separator IN BOOLEAN DEFAULT FALSE, 
p_max_linesize IN NUMBER DEFAULT 32000, 
p_mode IN VARCHAR2 DEFAULT 'w' 
) 
RETURN NUMBER 
IS 

에 나는 드를 분리하기 위해 다음과 같은 정규식을 사용하려고 한 내가 나중에 스키마 이름을 지우거나 함수/프로 시저/패키지의 이름을 큰 따옴표로 묶지 않도록 수정 한 후에 나중에 재구성 할 수 있습니다. 나는 버퍼에 각을 받고 고민하고있다 - 여기에 자신의 버퍼에 모든 중간 입력/출력을 잡아 내 최신 시도의 :

\b(CREATE\sOR\sREPLACE\s(PACKAGE|PACKAGE\sBODY|PROCEDURE|FUNCTION))(?:\W+\w+){1,100}?\W+(RETURN)\s*(\W+\w+)\s(AS|IS)\b 

모든/모든 도움에 감사드립니다!

내가 수정 된 파일/쓰기 평가하기 위해 지금 사용하고 스크립트입니다 : var에 스칼라로 저장되는 파일의 전체 내용을 가정

#!/usr/bin/perl 
use strict; 
use warnings; 
use File::Find; 
use Data::Dumper; 

# utility to clean strings 
sub trim($) { 
    my $string = shift; 
    $string = "" if !defined($string); 

    $string =~ s/^\s+//; 
    $string =~ s/\s+$//; 

    # aggressive removal of blank lines 
    $string =~ s/\n+/\n/g; 
    return $string; 
} 

sub cleanup_packages { 
    my $file = shift; 
    my $tmp = $file . ".tmp"; 
    my $package_name; 

    open(OLD, "< $file") or die "open $file: $!"; 
    open(NEW, "> $tmp") or die "open $tmp: $!"; 

    while (my $line = <OLD>) { 

    # look for the first line of the file to contain a CREATE OR REPLACE STATEMENT 
     if ($line =~ 
m/^(CREATE\sOR\sREPLACE)\s*(PACKAGE|PACKAGE\sBODY)?\s(.+)\s(AS|IS)?/i 
     ) 
     { 

      # look ahead to next line, in case the AS/IS is next 
      my $nextline = <OLD>; 

      # from the above IF clause, the package name is in buffer 3 
      $package_name = $3; 

      # if the package name and the AS/IS is on the same line, and 
      # the package name is quoted/prepended by the TRON2000 schema name 
      if ($package_name =~ m/"TRON2000"\."(\w+)"(\s*|\S*)(AS|IS)/i) { 
       # grab just the name and the AS/IS parts 
       $package_name =~ s/"TRON2000"\."(\w+)"(\s*|\S*)(AS|IS)/$1 $2/i; 
       trim($package_name); 
      } 
      elsif ( ($package_name =~ m/"TRON2000"\."(\w+)"/i) 
        && ($nextline =~ m/(AS|IS)/)) 
      { 

# if the AS/IS was on the next line from the name, put them together on one line 
       $package_name =~ s/"TRON2000"\."(\w+)"(\s*|\S*)/$1/i; 
       $package_name = trim($package_name) . ' ' . trim($nextline); 
       trim($package_name); # remove trailing carriage return 
      } 

      # now put the line back together 
      $line =~ 
s/^(CREATE\sOR\sREPLACE)\s*(PACKAGE|PACKAGE\sBODY|FUNCTION|PROCEDURE)?\s(.+)\s(AS|IS)?/$1 $2 $package_name/ig; 

      # and print it to the file 
      print NEW "$line\n"; 
     } 
     else { 

      # just a normal line - print it to the temp file 
      print NEW $line or die "print $tmp: $!"; 
     } 
    } 

    # close up the files 
    close(OLD) or die "close $file: $!"; 
    close(NEW) or die "close $tmp: $!"; 

    # rename the temp file as the original file name 
    unlink($file) or die "unlink $file: $!"; 
    rename($tmp, $file) or die "can't rename $tmp to $file: $!"; 
} 

# find and clean up oracle files 
sub eachFile { 
    my $ext; 
    my $filename = $_; 
    my $fullpath = $File::Find::name; 

    if (-f $filename) { 
     ($ext) = $filename =~ /(\.[^.]+)$/; 
    } 
    else { 

     # ignore non files 
     return; 
    } 

    if ($ext =~ /(\.spp|\.sps|\.spb|\.sf|\.sp)/i) { 
     print "package: $filename\n"; 
     cleanup_packages($fullpath); 
    } 
    else { 
     print "$filename not specified for processing!\n"; 
    } 
} 

MAIN: 
{ 
    my (@files, $file); 
    my $dir = 'C:/1_atest'; 

    # grab all the files for cleanup 
    find(\&eachFile, "$dir/"); 

    #open and evaluate each 
    foreach $file (@files) 
    { 
     # skip . and .. 
     next if ($file =~ /^\.$/); 
     next if ($file =~ /^\.\.$/); 
      cleanup_file($file); 
     }; 
} 

답변

3

을하여 어떻게해야 다음 장난.

$Str = ' 
CREATE OR REPLACE FUNCTION "TRON2000"."DC_F_DUMP_CSV_MMA" (
    p_trailing_separator IN BOOLEAN DEFAULT FALSE, 
    p_max_linesize IN NUMBER DEFAULT 32000, 
    p_mode IN VARCHAR2 DEFAULT w 
) 
RETURN NUMBER 
IS 

CREATE OR REPLACE FUNCTION "TRON2000"."DC_F_DUMP_CSV_MMA" (
    p_trailing_separator IN BOOLEAN DEFAULT FALSE, 
    p_max_linesize IN NUMBER DEFAULT 32000, 
    p_mode IN VARCHAR2 DEFAULT w 
) 
RETURN NUMBER 
IS 
'; 

$Str =~ s#^(create\s+(?:or\s+replace\s+)?\w+\s+)"[^"]+"."([^"]+)"#$1 $2#mig; 

print $Str; 
+0

우수 @tuxuday! 매우 효율적이고 훨씬 덜 복잡합니다 - 감사합니다! 지금 내가 정리해야 할 문자열이'TRON2000.DC_F_DUMP_CSV_MMA'에서'DC_F_DUMP_CSV_MMA' (스키마 이름은 있지만 이중 따옴표는 없습니다) 인 경우에 약간 수정하려고합니다. 다시 감사합니다! –

관련 문제