【问题标题】:sed replace a word at a line which begins with a specific pattern usingsed 替换以特定模式开头的行中的单词
【发布时间】:2013-09-12 16:50:34
【问题描述】:

如何在 FreeBSD 上替换以特定模式开头的行中的单词? 考虑以下文件内容:

this is to test 
that was for test

我想在以“this”开头的行替换“test”。

【问题讨论】:

    标签: sed


    【解决方案1】:

    要替换以this 开头的行,请说:

    $ sed '/^this/ s/test/something/' inputfile
    this is to something 
    that was for test
    

    这将在以this 开头的行上用something 替换单词test

    如果要替换匹配行上的所有 test 实例,请向 sed 提供 g 选项:

    sed '/^this/ s/test/something/g' inputfile
    

    【讨论】:

      【解决方案2】:

      要就地进行更改,请使用以下命令:

      sed -i '/^this/ s/test/something/g' inputfile;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-15
        • 1970-01-01
        • 1970-01-01
        • 2019-07-26
        • 1970-01-01
        • 1970-01-01
        • 2019-01-22
        • 1970-01-01
        相关资源
        最近更新 更多