【问题标题】:how to replace the text line with empty line如何用空行替换文本行
【发布时间】:2017-06-19 09:44:03
【问题描述】:

cat fxmgr/wrapperwin32.xml | sed '//d' | sed '//d' > clean.xml

这将从 xml 文件中删除 cmets,但我想用空行替换 cmets,因为我不想打扰其他代码行号。

例如:

line one
xml comment
xml comment
line four

输出:

line one

line four

假设 2 和 3 在代码中有 xml cmets。我们只是删除这些行并保持空行。 我无法在这里添加确切的 xml cmets。所以希望上面的 ex 澄清它。

cmets 中给出的真实示例:

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!---Students grades are uploaded by months----> 
3 <class_list> 
4 <student> 
5 <name>Tanmay</name> 
6 <grade>A</grade> 
7 </student> 
8 </class_list>

输出

1 <?xml version="1.0" encoding="UTF-8" ?> 
2 
3 <class_list> 
4 <student> 
5 <name>Tanmay</name> 
6 <grade>A</grade> 
7 </student> 
8 </class_list

目标:将注释第 2 行的内容替换为空格(以及与第 2 行类似的 xml 文件中的所有其他注释行)

【问题讨论】:

  • 您介意提供一些示例输入文本和所需的输出吗?
  • 1 2 3 4 5 Tanmay 6 A 7 8 输出 ===== 1 2 3 4 5 Tanmay 6 A 7 8
  • 1 2 3 4 5 Tanmay 6 A 7 8 输出 ===== 1 2 3 4 5 Tanmay 6 A 7 8
  • 请添加这个有问题的。很难理解
  • 我在问题中添加了。

标签: xml bash sed grep


【解决方案1】:

要替换您的 xml 文件中根据您的注释具有以下格式的注释行:

2 <!---Students grades are uploaded by months----> 

你可以使用

sed 's/<!--.*-->/ /g' test.xml

s/ : sed 的替换操作(格式 's/oldtext/newtext/g') 提示:作为 newtext,我们只需放置一个空格。

/g:用于全局替换所有行。

您可以使用 sed -i 将更改应用到同一文件,也可以通过在 sed 命令末尾附加 &gt;newfile.xml 将输出重定向到另一个文件。

【讨论】:

    【解决方案2】:

    从谷歌搜索我得到了。

    这是删除 XML 文件中注释行的命令。

    cat fxmgr/wrapperwin32.xml | sed '/<!--.*-->/d' | sed '/<!--/,/-->/d' > cleaned.xml
    

    但我想用空格替换这个 cmets。

    【讨论】:

      【解决方案3】:

      尝试使用正则表达式模式匹配来查找和删除。

      cat fxmgr/wrapperwin32.xml | sed '/\/\//d' > cleaned.xml
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        • 2012-06-24
        • 1970-01-01
        相关资源
        最近更新 更多