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

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM