随笔 - 126  文章 - 71 评论 - 4 阅读 - 25万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

 

 

 

 

posted on   e_shannon  阅读(5197)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示