2

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'),...

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok can you show me output of grep Local "${staging_sql_file}" | cat -vte command
Unfortunately not, because the result it's huge.
Oh that is to save original file with .bak extension in case something goes wrong in editing.
Oh ok ! But I don't mind to backup the original file, as the original files is always a fresh copy of my local database.
ok but that is unimportant here. Main thing is replacement should work for you and only reason it doesn't work if sql file has some invalid characters
|
1
[SO]$ cat file
Site Title - Local
Site Title - Staging
Site Title

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.