LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Blogs > zhjim
User Name
Password

Notices

Rate this Entry

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.
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];

openSQL'+< ./xx02' );
while ( 
$go >= ){
        
$line rl( *SQL );
        
$go--;

        
# find line CREATE DATABASE
        
if ( fl$line'CREATE DATABASE' ) >= ){
        
# get pos in fileo
        # substract length of line
        # put into start
                
$start sysseekSQL0) - length$line );
        }

        if ( 
fl$line'USE ' ) >= ){
        
# find line Use Db
        # get pos in file
        # substrat length of line
        # get end
                
$end sysseekSQL0);
                
$go 0;
        }
}

my $new 'CREATE DATABASE ' $db "\n";
$new .= 'USE ' $db "\n";

my $pad $end $start length($new) -1;
if ( 
$pad <= ){
        print( 
"New name is to big" );
        exit -
2;
}
while ( 
$pad ){
        
$new .= ' ';
        
$pad--;
}
$new .= "\n";

# Set filehandle to starting postiton
sysseekSQL$start);
syswriteSQL$new );

closeSQL );


        
# see if new ones fit into old space.
        # create new string
        # fill with zero
        # write back

sub fl {
        
my ($line$phrase) = @_;
        if ( 
substr$line0length$phrase )) eq $phrase ){
                return 
1;
        } else {
                return 
0;
        }
}

sub rl{
        
my $handle shift;
        
my $char;
        
my $line;

        while ( 
$char ne "\n" ){
                
sysread$handle$char);
                
$line .= $char;
        }
        return 
$line;

Posted in Uncategorized
Views 773 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 06:20 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration