【问题标题】:replace text in UNIX SED替换 UNIX SED 中的文本
【发布时间】:2012-04-22 13:02:24
【问题描述】:

我有 text1.txt 包含

<this can be anything, any number of line - no hardcoded>
<this can be anything, any number of line - no hardcoded>
     param1=<this can be any value - no hardcoded>
<this can be anything, any number of line - no hardcoded>
<this can be anything, any number of line - no hardcoded>

我想用 abc

替换这部分 这可以是任何值 - 没有硬编码

正确的 SED 命令应该是什么? 请注意,由于环境限制,我无法灵活使用任何语言,例如 perl、python 或 ruby​​ ~ 并且只能使用 Unix 命令。 并且除了 param1

之外没有硬编码

提前非常感谢!

【问题讨论】:

  • This SO question 可能会有所帮助。
  • 评论中的所有样本值怎么样?
  • @shiplu 你能否澄清一下评论的构成,根据 OP 的问题,text1.txt 是一个普通的文本文件,所以任何东西都可以表明评论?
  • 我向 OP 提出了这个问题以进行澄清

标签: regex unix sed


【解决方案1】:

听起来你想要s/&lt;.\+&gt;$/&lt;abc&gt;/

$ cat test1.txt

<this can be anything, any number of line - no hardcoded>
<this can be anything, any number of line - no hardcoded>
     param1=<this can be any value - no hardcoded>
<this can be anything, any number of line - no hardcoded>
<this can be anything, any number of line - no hardcoded>

$ sed 's/<.\+>$/<abc>/' test1.txt

<abc>
<abc>
     param1=<abc>
<abc>
<abc>

【讨论】:

    【解决方案2】:

    你想要的调用是

    sed -e 's/param1=.*/param1=abc/' text1.txt
    

    【讨论】:

    • 这可能会更改注释中的所有示例值。没有?
    • 如果您的意思是六字符序列param1= 将始终在其行中的其余字符替换为abc,即使它出现在“评论”中也是如此。但是 OP 从来没有说过任何关于 cmets 的事情。
    猜你喜欢
    • 2012-10-14
    • 2014-12-22
    • 1970-01-01
    • 2017-12-20
    • 2011-11-25
    • 2022-10-21
    • 1970-01-01
    • 2016-09-04
    相关资源
    最近更新 更多