KodeFor.Me
KodeFor.Me

Reputation: 13511

How to replace a string inside an SQL file with sed

I have a Multi-site WordPress installation, that contains several sites for the company I work for, and I have produce a bash script, that extracting the database tables for each site separately.

In addition, we title each site based on the location of each copy. In example the local copy of a site it is named like Site Title - Local, the staging has a title like Site Title - Staging and the production like Site Title.

In my script I can use the following sed command in order to find and replace the - Local to - Staging

sed -i -e "s/\s\-\sLocal/\ \-\ Staging/g" ${staging_sql_file};

But the following doesn't work for removing the string - Local:

sed -i -e "s/ - Local//g" ${staging_sql_file};

I also have try patterns like the following ones:

"s/\s\-\sLocal//g"
"s/ \- Local//g"
"s/\s\-\sLocal/\s/g"
"s/ \- Local/ /g"

but none of these works.

Can somebody help me with that problem ?

UPDATE #1

When I try the regex's I display above, I get an error from sed.

UPDATE #2

My SQL files are like the following one:

LOCK TABLES `wp_16_options` WRITE;
/*!40000 ALTER TABLE `wp_16_options` DISABLE KEYS */;
INSERT INTO `wp_16_options` VALUES (1,'siteurl','https://www.site-url.com','yes'),(2,'home','https://www.site-url.com','yes'),(3,'blogname','Site Title - Local','yes'),(4,'blogdescription','Just another multi site wp site','yes'),(5,'users_can_register','0','yes'),...

Upvotes: 2

Views: 3644

Answers (2)

anubhava
anubhava

Reputation: 785226

Try this sed:

sed -i.bak 's/[[:blank:]]*-[[:blank:]]*Local//g' "${staging_sql_file}"

i.e. use of POSIX class [[:blank:]] in place of a horizontal white-space.

Upvotes: 1

Amit
Amit

Reputation: 20456

[SO]$ cat file
Site Title - Local
Site Title - Staging
Site Title

[SO]$ sed 's/ - Local//g' file
Site Title
Site Title - Staging
Site Title

Upvotes: 1

Related Questions