Skip to content

Instantly share code, notes, and snippets.

@shandanjay
Created May 18, 2015 06:52
Show Gist options
  • Save shandanjay/25148c1cb66dad4d242f to your computer and use it in GitHub Desktop.
Save shandanjay/25148c1cb66dad4d242f to your computer and use it in GitHub Desktop.
Split databases from a bulk db sql file created by PhpMyAdmin. (the sql file name need to be "localhost.sql")
use Tie::File;
use IO::Handle;
tie @SQL_FILE, 'Tie::File' , "localhost.sql" or die $!;
mkdir('output');
STDOUT->printflush("Processing");
for $lineNum (1 .. $#SQL_FILE){
if($SQL_FILE[$lineNum] =~ /\-\- Database: `(.*)`/){
$j = $lineNum;
$currentDBName = $1;
$currentFileContent = "";
STDOUT->printflush(".");
$j++;
while($SQL_FILE[$j] !~ /\-\- Database: `(.*)`/ && $j<=$#SQL_FILE) {
$currentFileContent .= $SQL_FILE[$j]. "\n";
$j++;
}
open NEW_FILE , ">", "output/"."$currentDBName.sql" or die "Cannot open output file";
print NEW_FILE $currentFileContent ;
$currentFileContent = "";
}
}
untie @SQL_FILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment