Perl script to modify the file

I have Oracle files that I need to compare with CVS files, but the problem is that there are many files that I want to ignore in the first line (s) as part of the diff. I want to run a script that opens each file and replaces the contents of the file so that the final output replaces 'CREATE OR REPLACE PACKAGE "TRON"."SOME_PACKAGE" IS'with 'CREATE OR REPLACE PACKAGE SOME_PACKAGE IS'. The problem I am facing is that the statement can span multiple lines, so I have to consider a type situation .'CREATE OR REPLACE "TRON"."SOME_PACKAGE"
IS'

My approach (since this is part of Jenkins' job) is to iterate over all the files in the workspace, modifying any files that meet these criteria. Then I can use my existing Perl script, which uses File::Compareand Text::Diff::Table.

I tested the Zaid solution with little success, as it still has no problems with scripts where the command line spans multiple lines. (my changes):

use strict;
use warnings;
use Tie::File;
use Data::Dumper;

my @array;

tie @array, 'Tie::File', 'c:\cb_k_check_recon_mma.sps' or die "Unable to tie file";

my %unwanted = map  { $_ => 1 }
               map  { $_-1..$_-4, $_, $_+2 .. $_+4 }
               grep { $array[$_] =~ /^CREATE.*[IS|AS]$/ }
               0 .. $#array ;

print Dumper \%unwanted;

@array = map { $array[$_] } grep { ! $unwanted{$_} } 0 .. $#array;
print Dumper \@array;

untie @array;
+1
source share
1 answer

If the text can span multiple lines, for a single regular expression to work, you need to read the file in line, not line.

perl -0777 -pi.bak -e 's/CREATE\s+OR\s+REPLACE\s+PACKAGE\s+"TRON"\."SOME_PACKAGE"\s+IS/CREATE OR REPLACE PACKAGE SOME_PACKAGE IS/g' /path/*.pl

The switch -0777tells perl to debug the file, so the regular expression will only run once. For this reason, I added a global modifier /gif more than one lookup is required for each file.

, \s+ , . -pi , (), .bak -i . , ( Windows).

+1

All Articles