Perl Skript to change the heading of an sql dump
Posted 08-11-2009 at 04:19 AM by zhjim
Needed to have a programm that would allow me to change the headers of a sql dump.
This is the first draft and far from perfect but does the job.
This is the first draft and far from perfect but does the job.
PHP Code:
#!/usr/bin/perl
use strict;
use Fcntl;
my $line;
my $go = 10;
my $start;
my $end;
if ( $#ARGV + 1 != 1 ) {
print 'Usage: trial.pl new_db_name' . "\n";
exit -1;
}
my $db = $ARGV[0];
open( SQL, '+< ./xx02' );
while ( $go >= 1 ){
$line = rl( *SQL );
$go--;
# find line CREATE DATABASE
if ( fl( $line, 'CREATE DATABASE' ) >= 1 ){
# get pos in fileo
# substract length of line
# put into start
$start = sysseek( SQL, 0, 1 ) - length( $line );
}
if ( fl( $line, 'USE ' ) >= 1 ){
# find line Use Db
# get pos in file
# substrat length of line
# get end
$end = sysseek( SQL, 0, 1 );
$go = 0;
}
}
my $new = 'CREATE DATABASE ' . $db . "\n";
$new .= 'USE ' . $db . "\n";
my $pad = $end - $start - length($new) -1;
if ( $pad <= 0 ){
print( "New name is to big" );
exit -2;
}
while ( $pad > 0 ){
$new .= ' ';
$pad--;
}
$new .= "\n";
# Set filehandle to starting postiton
sysseek( SQL, $start, 0 );
syswrite( SQL, $new );
close( SQL );
# see if new ones fit into old space.
# create new string
# fill with zero
# write back
sub fl {
my ($line, $phrase) = @_;
if ( substr( $line, 0, length( $phrase )) eq $phrase ){
return 1;
} else {
return 0;
}
}
sub rl{
my $handle = shift;
my $char;
my $line;
while ( $char ne "\n" ){
sysread( $handle, $char, 1 );
$line .= $char;
}
return $line;
}
Total Comments 0