【问题标题】:Using sed in red hat linux to replace text在red hat linux中使用sed替换文本
【发布时间】:2015-01-22 17:16:49
【问题描述】:

我在一个目录中有一些 XML 文件,它们都包含标签:<difficult>0</difficult>。我只是想把它改成<difficult>1</difficult>

我正在使用以下命令:

sed 's/difficult>0/difficult>1/g' *.xml

所发生的只是显示所有文件的完整 XML 文本,difficult 标记显示值为 1,但实际文件没有任何反应。当我打开它们时,它们仍然包含<difficult>0</difficult>

【问题讨论】:

标签: linux sed


【解决方案1】:
sed -i 's/difficult>0/difficult>1/g' *.xml

Change a string in a file with sed?

【讨论】:

  • 您还没有提名重复的权限,但一般来说,如果您发现重复,请留下评论并附上链接而不是回答。谢谢。
【解决方案2】:

是的,sed 通常将其结果放在stdout 上。要就地更改文件,请使用-i 标志:

  -i[SUFFIX], --in-place[=SUFFIX]

          edit files in place (makes backup if SUFFIX supplied)

【讨论】:

    【解决方案3】:

    再来一次:)

    不要使用正则表达式来解析 HTML。使用正确的解析器 & :

    xml ed -L -u '//difficult/text()' -v "1" file
    

    xml

    查看:RegEx match open tags except XHTML self-contained tags

    【讨论】:

      猜你喜欢
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 2013-03-03
      • 2011-11-02
      相关资源
      最近更新 更多