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
1 Answer
Reset to default 2Instead 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
版权声明:本文标题:wp cli - WP CLI: search and replace specific directory name of URL 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741230164a2361973.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论