Howto split a SQL database dump into table-wise files
Splitting a sql file containing a whole database into per-table files is quite easy:
- Grep the .sql for any occurence of DROP TABLE.
- Generate the file name from the table name that is included in the DROP TABLE statement.
- Echo the output to a file.
Here is a little script that expects a .sql file as input:
#!/bin/bash file=$1 # the input file directory="$file-splitted" # the output directory output="$directory/header" # the first file containing the header GREP="DROP TABLE" # what we are looking for mkdir $directory # create the output directory while read line do # if the current line contains the wanted statement if [ $(echo "$line" | grep -c "$GREP") == "1" ] then # extract the file name myfile=$(echo $line | awk '{print $5}' | sed -e 's/`//g' -e 's/;//g') # set the new file name output="$directory/$myfile" fi echo "$line" >> $output # write to file done < $file
Here you can download the script if you don’t want to copy & paste. More here.




Eberle, Lars
Schneider, Carsten
Härtel, Torsten








