Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: how to easily edit one line in a 5.4G SQL dump [closed]

Tags:

bash

mysql

We're running into issues where we need to work on recovering some lost data (yay improper use of maatkit on a master-to-master replication setup), and I'm trying to import an old dump of the database. The problem is that at the top of the file, it's explicitly specifying the database (mysqldump was run with the --all-databases option), and I need to change that DB to something else so I can get another in there alongside it for comparison. The line reads:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `dms` /*40100 DEFAULT CHARACTER SET latin1 */

I've been unsuccessful at opening the file in vi to edit due to the large size of the dump, and am a little hesitant to use sed because of how it's going to read line-by-line for any pattern matching. What is the easiest and most efficient way to change the above line in the sql dump to read:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `dms_old` /*40100 DEFAULT CHARACTER SET latin1 */

Or simply use some mysql black magic to import it into database dms_old?

like image 214
Scott Avatar asked Nov 21 '11 22:11

Scott


2 Answers

sed should have no problems with that. Just do:

`sed '/CREATE DATABASE/s/dms/dms_old/'`

with appropriate redirections.

like image 121
ninjalj Avatar answered Nov 11 '22 01:11

ninjalj


I think this is right.

sed '0,/dms/s/dms/dms_old/' dump.sql

That will only run the replacement once, and only for the first line.

like image 3
Jonathan Avatar answered Nov 11 '22 03:11

Jonathan