zoukankan      html  css  js  c++  java
  • sed:在匹配模式的行首或者行尾插入字符

    info sed,可以看到更多

    https://www.onitroad.com/jc/misc/insert-character-in-the-beginning-or-end-of-line-with-matched-pattern-in-sed.html

    shannon:这个网站好像不错

    sed:在匹配模式的行首或者行尾插入字符

    示例文件“/tmp/file”

    1
    2
    3
    4
    5
    6
    7
    # Port rpc.statd should listen on.
    STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2016
    Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo"

    在行首添加内容

    示例1

    在包含“STATD#u PORT”的行开头添加“#”注释符号

    解决方案

    1
    2
    3
    4
    5
    6
    7
    8
    # sed '/STATD_PORT/s/^/#/' /tmp/file
    # Port rpc.statd should listen on.
    #STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2016
    Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo"

    将更改结果保存到文件:

    1
    # sed -i '/STATD_PORT/s/^/#/' /tmp/file

    示例 2

    要匹配的内容在行的中间

    匹配“callout”并在行首添加“#”注释字符

    解决方案

    1
    2
    3
    4
    5
    6
    7
    8
    # sed '/callout/s/^/#/' /tmp/file
    # Port rpc.statd should listen on.
    STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2016
    #Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo"

    在行尾添加内容

    示例 1

    在与“callout”匹配的行的末尾添加“your text”

    1
    2
    3
    4
    5
    6
    7
    8
    # sed '/callout/s/$/your text/' /tmp/file
    # Port rpc.statd should listen on.
    STATD_PORT=662
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2016
    Specify callout program your text
    STATD_HA_CALLOUT="/usr/local/bin/foo"

    示例 2

    在以“STATD”开头的行的 末尾添加“your text”

    1
    2
    3
    4
    5
    6
    7
    8
    # sed '/^STATD/s/$/your text/' /tmp/file
    # Port rpc.statd should listen on.
    STATD_PORT=662 your text
    Outgoing port statd should used. The default is port
    # is random
    STATD_OUTGOING_PORT=2016 your text
    Specify callout program
    STATD_HA_CALLOUT="/usr/local/bin/foo" your text
  • 相关阅读:
    HDU 5861 Road (线段树)
    HDU 5857 Median (推导)
    HDU 5858 Hard problem (数学推导)
    HDU 5867 Water problem (模拟)
    UVALive 7455 Linear Ecosystem (高斯消元)
    A bug about RecipientEditTextView
    当Activity出现Exception时是如何处理的?
    FontSize sp 和 dp 的区别
    Android的Overlay机制
    关于控件问题的分析
  • 原文地址:https://www.cnblogs.com/e-shannon/p/15165055.html
Copyright © 2011-2022 走看看