Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a table from a mysql.sql dump file [duplicate]

I have a products table in a mysql.sql file. I wish to extract that one table and place it in it's own file. How would you go about doing this?

like image 441
Ryan Avatar asked Oct 14 '10 17:10

Ryan


2 Answers

I ran into that problem a while ago and wrote a Perl script. It worked well, but it was an older version of MySQL. Call it like:
extract.pl -table=TABLENAME mysqldumpfile.sql > recovered_table.sql

#!/usr/bin/perl -s -wnl
#extract a single table from a mysql dump
BEGIN {
    $table or warn
    "Usage: $0 -table=TABLE_TO_EXTRACT mysqldumpfile.sql"
    and exit 1;
}

/^DROP TABLE IF EXISTS `$table`/ .. /^UNLOCK TABLES;$/ and print;
like image 74
charlesbridge Avatar answered Oct 09 '22 16:10

charlesbridge


I found this nice solution, you have to download this script on your server and type

$> ./MyDumpSplitter.sh yourfile.sql your_table_name

This will extract your table into your_table_name.sql


Optionnal

Now you can rename it using this type of command

$> sed -i 's/`your_table_name`/`your_table_name2`/g' your_table_name.sql

And re-injecting it with

mysql> source your_table_name.sql;
like image 39
Pierre de LESPINAY Avatar answered Oct 09 '22 14:10

Pierre de LESPINAY