Bash: how to easily edit a single row in a 5.4G dump of SQL

We are having problems when we need to work on recovering some lost data (improper use of maatkit when setting up Master-to-master replication), and I'm trying to import an old database dump. The problem is that at the top of the file it explicitly indicates the database (mysqldump was started with the -all-databases option), and I need to change this database to something else so that I can find another there, next to it, for comparison. The line reads:

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

I could not open the file in vi for editing due to the large dump size, and it is a bit hesitant to use sed because of how it will read line by line for any pattern matching. The easiest and most efficient way to change the above line in a sql dump:

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

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

+6
source share
2 answers

sed should not be a problem with this. Just do:

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

with appropriate redirects.

+4
source

I think that is correct.

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

This will only work once and only for the first line.

+3
source

Source: https://habr.com/ru/post/902065/


All Articles