Extracting a single database from a mysqldump

Some days ago a friend asked me how to extract a single database from a complete database dump. He created the dump using mysqldump and only wanted to restore a single database, without having go through >80k lines.

After a little thinking I came up with a little script that does the work:



I used grep to find the line of where the database is created and cut to extract the line number. Sed can be used to extract lines or a range of lines from a text file.

sed -n 1,10p file

This extracts lines 1 to 10 from a file. It's pretty amazing what sed can be used for..

Comments (4) | MySQL, Bash

Comments

Thesis Writing

Thank you for sharing such relevant topic with us. I really love all the great stuff you provide. Thanks again and keep it coming.

Buyviagraonline

Buyviagraonline
http://sasfxdsa.blogspot.com/
Buyviagraonline!!!

Conrad

# Conrad's version (that actually works for Conrad)
# usage: ./grep_db.sh dump.sql dbname > output.sql
line=`grep -m 1 -n "Current Database: .$2" $1 | cut -d ":" -f 1`
next=`sed 1,${line}d $1|grep -m 1 -n "Current Database" | cut -d ":" -f 1`
end=$(($line + $next -1))
sed -n ${line},${end}p $1

#Thanks Fabian!

Create Comment