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
|
