Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

String replacement preparation for file comparison

by TJRandall (Sexton)
on Apr 06, 2012 at 13:28 UTC ( #963823=perlquestion: print w/ replies, xml ) Need Help??
TJRandall has asked for the wisdom of the Perl Monks concerning the following question:

I need to compare CVS files to Oracle code extracts from our Production environment. The issue is that some of the CVS files do not declare the function (package, procedure) in the same format as the Oracle extract, which later flags the files as different when using Text:Diff.

This difference:

CREATE OR REPLACE FUNCTION DC_F_COD_RANDOM_MMA RETURN VARCHAR2 IS

vs.

CREATE OR REPLACE FUNCTION "TRON2000"."DC_F_COD_RANDOM_MMA" RETURN VARCHAR2 IS

is one that I can safely ignore.

My approach is to open each CVS file, and check to see if it declares in the Oracle format. If not, replace the current declaration with a cleaned up Oracle one. (this is actually my first question - does this sound like a valid approach?)

So I look for the piece between 'CREATE OR REPLACE' and 'AS' or 'IS'. Inside there, I will find 'FUNCTION', 'PROCEDURE' or 'PACKAGE', and the actual name. So in the code I'm working on below, I have a function which is not in the Oracle format - so I have to change the file contents to

CREATE OR REPLACE FUNCTION "TRON2000"."DC_F_COD_RANDOM_MMA" RETURN VARCHAR2 IS

Is there a way to do this cleanly / easily, without all the IF/ELSE blocks that I'm building out - my current approach feels like I'm going to be sprawling quickly into something that is unmanageable.

Your thoughts / pointers / advice is GREATLY appreciated!

#!/usr/bin/perl -w use strict; use warnings; use 5.010; $/=undef; my $start_search_str = 'CREATE OR REPLACE '; while (<DATA>) { my $rc; # if the file contents are in the Oracle extract form if( $rc = /$start_search_str (.+) \"TRON2000\"\.\"/ .. /(AS|IS)/ +){ # file is the right format - get the next one last; } else { # nope - need to fix them - first - get the part of the string # between 'CREATE OR REPLACE ' and (AS|IS), then insert # "TRON2000"."<name>" around the name my $contents = $_; $contents =~ /(($start_search_str)(.*)(AS|IS))/si; my $function_name = $3; if ($3 =~ /FUNCTION/si) { $function_name =~ /( FUNCTION(.*) RETURN)/si; print "NOW: function_name: $function_name\n"; # now substitute and write it back to $rc }; }; } __DATA__ CREATE OR REPLACE FUNCTION DC_F_COD_RANDOM_MMA RETURN VARCHAR2 IS -- l_random_code1 VARCHAR2(1) ; RETURN l_random_code ; -- END dc_f_cod_random_mma ;

Comment on String replacement preparation for file comparison
Select or Download Code
Re: String replacement preparation for file comparison
by roboticus (Canon) on Apr 06, 2012 at 13:47 UTC

    TJRandall:

    I recently had a problem where someone *radically* reformatted a package, and I had to add one of their patches to the non-screwed-up version. So to find the differences, I reformatted *both* versions and diffed the results. Then I could identify *what* the changes were, so I went back to my original version and applied the appropriate patch.

    The reformatter was trivial: I eliminated all line breaks, then I inserted line breaks after each semicolon and comma, like so:

    $ cat squidge.pl #!/usr/bin/perl use strict; use warnings; use File::Slurp; my $INFName = shift or die "Missing file name"; my $t = read_file($INFName); $t =~ tr/\n\r/ /; $t =~ s/;/;\n/g; $t =~ s/,/,\n/g; open my $FH, '>', $INFName . ".out" or die; print $FH $t, "\n";

    Then I used GNU diff, telling it to ignore whitespace differences and such:

    diff -abiwEB --strip-trailing-cr file1 file2

    If you're just trying to identify files that differ, this might do the trick for you.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      This is a great approach - thank you! This fixes my "other" compare problem (differences flagged in Text::Diff on carriage returns). I still have to figure out the first "fix" - taking the function/package/procedure name and making sure it is:

      CREATE OR REPLACE FUNCTION "TRON2000"."<name>" AS.

      Thank you again!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963823]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-10-26 05:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (151 votes), past polls