用于更改序列化 PHP 数据的 Bash 脚本

发布于 2024-12-25 17:38:38 字数 263 浏览 2 评论 0原文

我有许多包含 php 序列化数据的文件,其中我必须用另一个字符串替换一些字符串。 Linux主机没有安装任何php。问题是将修改后的字符串调整为正确的大小。

我尝试了类似将 /share 路径替换为 /opt:

sed -re 's~s:([0-9]+):"/share([^"]*)~s:int(\1-2):/opt\2~g' file

但结果文件很糟糕:长度是垃圾表达式 int(size - 2)

知道吗?

I have many files containing php serialized data in which I have to replace some strings by another one. The linux host doesn't have any php installed. The problem is to adjust the modified string to correct size.

I tried something like to replace /share path to /opt:

sed -re 's~s:([0-9]+):"/share([^"]*)~s:int(\1-2):/opt\2~g' file

but the result file is bad: lengths are litteral expression int(size - 2)

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2025-01-01 17:38:38

这个解决方案并不理想,但您可以使用 perl:

my $line;
while ($line = <STDIN>) {
    $line =~ s~s:([0-9]+):"/share([^"]*)~"s:".($1-2).":\"/opt$2"~ge;
    print $line;
}

希望我已经正确理解了您的要求。这是一个示例:

php -r 'echo serialize(array("/share/foo")) . "\n";'
a:1:{i:0;s:10:"/share/foo";}

php -r 'echo serialize(array("/share/foo")) . "\n";' | perl replace.pl
a:1:{i:0;s:8:"/opt/foo";}

编辑:这是一个修改后的脚本,用于通过变量搜索和替换就地编辑文件字符串

This solution isn't ideal, but you could use perl:

my $line;
while ($line = <STDIN>) {
    $line =~ s~s:([0-9]+):"/share([^"]*)~"s:".($1-2).":\"/opt$2"~ge;
    print $line;
}

Hopefully I've understood your requirements correctly. Here's an example:

php -r 'echo serialize(array("/share/foo")) . "\n";'
a:1:{i:0;s:10:"/share/foo";}

php -r 'echo serialize(array("/share/foo")) . "\n";' | perl replace.pl
a:1:{i:0;s:8:"/opt/foo";}

EDIT: Here's a modified script to edit the file in-place with variable search and replace strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文