Skip to content

Instantly share code, notes, and snippets.

@kmatt
Created June 8, 2012 19:45
Show Gist options
  • Save kmatt/2897778 to your computer and use it in GitHub Desktop.
Save kmatt/2897778 to your computer and use it in GitHub Desktop.
Split MySQL dumps
file=$1
nextDB=""
nextStart=0
nextEnd=0
lastDB=""
lastStart=0
for i in `grep -n "^CREATE DATABASE" $file | awk '{print $1":"$7}' | sed "s/CREATE://g"`
do
i=`echo $i | sed "s/\\\`//g"`
nextDB=`echo $i | cut -d : -f 2`
nextStart=`echo $i | cut -d : -f 1`
nextEnd=$(( $nextStart -1 ))
if [ "$lastDB" == "" ]
then
header=`sed -n 1,${nextEnd}p $file`
else
echo "$lastDB from $lastStart to $nextEnd"
echo "$header" > _$lastDB.sql
sed -n ${lastStart},${nextEnd}p $file >> _$lastDB.sql
fi
lastDB=$nextDB
lastStart=$(( $nextStart ))
done
echo "$lastDB from $lastStart"
echo "$header" > _$lastDB.sql
sed -n ${lastStart},\$p $file >> _$lastDB.sql
@kmatt
Copy link
Author

kmatt commented Jun 8, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment