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

 

 

 

 

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

上一篇:大数据开发技术之Hive数据仓库架构分层


下一篇:MySQL中一些关于索引的知识点