【问题标题】:Need to delete first N lines of grep result files需要删除前N行grep结果文件
【发布时间】:2016-12-21 15:45:34
【问题描述】:

我正在尝试解决我的一些 wordpress 安装中的黑客问题。

这家伙把 9 行代码放在我服务器上多个文件的头部...我正在尝试使用 grep 和 sed 来解决这个问题。

我正在尝试:

grep -r -l  "//360cdn.win/c.css" | xargs -0 sed -e '1,9d' < {}

但是什么都没有发生,如果我删除 -0 fromxargs, the result of the files found are clean, but they are not overwriting the origin file with thesed` 结果,有人可以帮我吗?

非常感谢!

【问题讨论】:

    标签: linux wordpress bash sed grep


    【解决方案1】:

    您应该在grep 命令中使用--null 选项在grep 输出中的每个文件名之后输出一个NUL 字节或\0。还可以在sed 中使用-i.bak 对每个文件进行内联编辑:

    grep -lR --null '//360cdn.win/c\.css' . | xargs -0 sed -i.bak '1,9d'
    

    【讨论】:

    • 非常感谢!!!!我试图在过去的 4 小时内解决这个问题!! =D
    • @RenanCorrêaPinto 在Stack Overflow 上说“谢谢”的方式是接受(和/或赞成)一个答案。跨度>
    【解决方案2】:

    直接迭代文件有什么问题¹?

    您可能希望将-i 平面添加到sed 以便编辑文件in-place

    grep -r -l  "//360cdn.win/c.css" | while read f
    do
       sed -e '1,9d' -i "${f}"
    done
    

    ¹好吧,如果您的文件包含换行符等,您可能会遇到问题。 但是...如果您的网站包含带有换行符的文件,那么您可能还有其他问题...

    【讨论】:

      猜你喜欢
      • 2021-05-10
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 2021-06-11
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多