News

Howto split a SQL database dump into table-wise files

by Pauli, Marcus 13 July 2011 in Software Development

Splitting a sql file containing a whole database into per-table files is quite easy:

  1. Grep the .sql for any occurence of DROP TABLE.
  2. Generate the file name from the table name that is included in the DROP TABLE statement.
  3. 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.

Who We Are and What We Do

Meyer, Miller, Smith.

Head. Hands. Heart. We believe that there is a real connection between craftsmanship and communication.

As information and intelligence becomes the domain of computers, society will place more value on the one human ability that cannot be automated: Emotions.

People want to experience beauty, enjoy one’s work, feel passion, they want to interact with each other. We all want.

That’s why we believe that the future of brands is interaction, not commodity. It’s not something you buy, but something you participate in.

Top