SED匹配/替换URL并更新序列化的阵列计数 [英] SED Match/Replace URL and Update Serialized Array Count

查看:156
本文介绍了SED匹配/替换URL并更新序列化的阵列计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是sql转储文件中的示例片段。此特定行包含Wordpress PHP序列化数组的meta_value。在dev。,test。和qc中进行数据库还原期间。我正在使用sed环境将URL替换为相应的环境子域。

Below is an example snippet from a sql dump file. This specific row contains a meta_value of a Wordpress PHP serialized array. During database restores in dev., test., and qc. environments I'm using sed to replace URLs with the respective environment sub-domain.

INSERT INTO `wp_postmeta`
    (`meta_id`,
     `post_id`,
     `meta_key`,
     `meta_value`)
VALUES
    (527,
     1951,
     'ut_parallax_image',
     'a:4:{
          s:17:\"background-image\";
          s:33:\"http://example.com/background.jpg\";
          s:23:\"mobile-background-image\";
          s:37:\"www.example.com/mobile-background.jpg\";
      }')
;

但是,我需要扩展它以在替换后更正序列化数组中的字符串长度。

However, I need to extend this to correct the string length in the serialized arrays after replace.

sed -r -e "s/:\/\/(www\.)?${domain}/:\/\/\1${1}\.${domain}/g" "/vagrant/repositories/apache/$domain/_sql/$(basename "$file")" > "/vagrant/repositories/apache/$domain/_sql/$1.$(basename "$file")"

对于开发人员来说结果应该是这样的:

The result should look like this for dev.:

INSERT INTO `wp_postmeta`
    (`meta_id`,
     `post_id`,
     `meta_key`,
     `meta_value`)
VALUES
    (527,
     1951,
     'ut_parallax_image',
     'a:4:{
          s:17:\"background-image\";
          s:37:\"http://dev.example.com/background.jpg\";
          s:23:\"mobile-background-image\";
          s:41:\"www.dev.example.com/mobile-background.jpg\";
      }')
;

我希望不引入除sed以外的任何依赖关系。

I'd prefer to not introduce any dependancies other than sed.

推荐答案

WP-CLI在替换搜索期间处理序列化的PHP数组 http://wp-cli.org/commands/search-replace/ 。我想尝试一个本机shell解决方案,但是拥有WP-CLI最终还是值得的。

WP-CLI handles serialized PHP arrays during a search-replace http://wp-cli.org/commands/search-replace/. I wanted to try a native shell solution, but having WP-CLI was worth the extra overhead in the end.

这篇关于SED匹配/替换URL并更新序列化的阵列计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆