Created
May 18, 2015 06:52
-
-
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")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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