admin管理员组

文章数量:1279207

I am trying to use wp cli (with regex) to search and replace.

I want to change:

domain/wp-content/uploads/2020/04/asdnbahsdjhgzw/picture.jpg

to

domain/wp-content/uploads/2020/04/picture.jpg

So I want to get rid of the "asdnbahsdjhgzw" part.

Now I tried something like:

wp search-replace "domain/wp-content/uploads/(\d)/(\d)/(\d*)/" "domain/wp-content/uploads/\$1/\$2/" --precise --all-tables --skip-columns=guid --regex
  • (\d)/(\d)/ to match and "capture" the directory structure for media and thought I would be able to call them back in the replacement with /$1/$2/.
  • (\d*)/ is to capture the random directory name, asdnbahsdjhgzw/, and don't call it back in the replacement.

BUT

Unfortunately, this isn´t working as I had hoped. I get the following error:

syntax error near unexpected token `('

I also tried the backslash before the (

/\(\d\)/\(\d\)/\(\d*\)/

But no luck.

Perhaps someone knows what could be the issue, apart from me not knowing WP CLI ;)

thanks!

I am trying to use wp cli (with regex) to search and replace.

I want to change:

domain/wp-content/uploads/2020/04/asdnbahsdjhgzw/picture.jpg

to

domain/wp-content/uploads/2020/04/picture.jpg

So I want to get rid of the "asdnbahsdjhgzw" part.

Now I tried something like:

wp search-replace "domain/wp-content/uploads/(\d)/(\d)/(\d*)/" "domain/wp-content/uploads/\$1/\$2/" --precise --all-tables --skip-columns=guid --regex
  • (\d)/(\d)/ to match and "capture" the directory structure for media and thought I would be able to call them back in the replacement with /$1/$2/.
  • (\d*)/ is to capture the random directory name, asdnbahsdjhgzw/, and don't call it back in the replacement.

BUT

Unfortunately, this isn´t working as I had hoped. I get the following error:

syntax error near unexpected token `('

I also tried the backslash before the (

/\(\d\)/\(\d\)/\(\d*\)/

But no luck.

Perhaps someone knows what could be the issue, apart from me not knowing WP CLI ;)

thanks!

Share Improve this question asked Nov 8, 2021 at 12:39 Elv1sElv1s 33 bronze badges 1
  • When I try your command, it works without any problems. Using WP CLI 2.5.0 in Alpine with Bash. – kero Commented Nov 8, 2021 at 14:35
Add a comment  | 

1 Answer 1

Reset to default 2

Instead of using " to wrap your expressions, use '.

Further experimenting shows me that this is incorrect—both " and ' should work, as long as they're matched (ie, you don't accidentally try something like wp search-replace "domain/wp-content/uploads/(\d)/(\d)/(\d*)/' "domain/wp-content/uploads/\$1/\$2/" --precise --all-tables --skip-columns=guid --regex. (Note that the regex in that example starts with " and ends with '.)

I'm not deleting this answer because the info below, about the regex itself, still stands.

The regex

Your regex looks wrong to me. \d will match a single digit, so it'd match, eg, 1, 5, etc, but not 2020 or 04.

I'd recommend something like this:

wp search-replace
--dry-run
'domain/wp-content/uploads/(\d+)/(\d+)/[^/]+/'
'domain/wp-content/uploads/\$1/\$2/'
--precise --all-tables --skip-columns=guid --regex

(line breaks added for readability; all this should be on a single line)

The [^/]+ will match everything but a / character in that last segment of the regex search. I also took out the () wrapping it since you're discarding it.

I also strongly recommend using the --dry-run argument with wp search-replace first, to see what you'll be replacing.

Edited again to add: I've removed the text about my deeper dive into using ' vs. " in bash, because it seems it was incorrect.

本文标签: wp cliWP CLI search and replace specific directory name of URL