侧边栏壁纸
  • 累计撰写 73 篇文章
  • 累计创建 23 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

【Linux命令】sed

Administrator
2025-06-23 / 0 评论 / 0 点赞 / 11 阅读 / 9500 字 / 正在检测是否收录...

sed命令详解

sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。

一、基本语法

sed [options] '{command}[flags]' [filename]    
# 中括号内容必有 大括号内容可有可无
sed  # 执行命令
[options]  # 命令选项
{command}[flags]    # sed内部选项和参数
[filename]     # 文件
# 命令选项
-e script 将脚本中指定的命令添加到处理输入时执行的命令中,  多条件,一行中要有多个操作
-f script 将文件中指定的命令添加到处理输入时执行的命令中
-n        抑制自动输出
-i        编辑文件内容
-i.bak    修改时同时创建.bak备份文件。
-r        使用扩展的正则表达式
!         取反 (跟在模式条件后与shell有所区别)
sed常用内部命令
a   在匹配后面添加
i   在匹配前面添加
p   打印
d   删除
s   查找替换
c   更改
y   转换   N D P 
flags
数字             表示新文本替换的模式
g:             表示用新文本替换现有文本的全部实例
p:             表示打印原始的内容
w filename:     将替换的结果写入文件

二、实操案例

# 新加一行

sed -i ' /<\/java-config>/i \ <jvm-options>-Dlog4j2.formatMsgNoLookups=true</jvm-options>' ${BES_HOME}/conf/server.config
# 判断环境变量(容器化环境)中是否有参数,有则替换
if [ -n "$jvmOptionsXmx" ];then
	sed -i "$(sed -n '/-Xmx/='${BES_HOME}/conf/server.config)""c\	$jvmOptionsXmx" ${BES_ HOME}/conf/server.config
fi
if [ -n "$jvmOptionsXms" ];then
	sed -i "$(sed -n '/-Xms/='${BES_HOME}/conf/server.config)""c\	$jvmOptionsXms" ${BES_HOME}/conf/server.config
fi

这个案例是我们项目中实际用到的,效果就是配置JVM参数。具体学习看下面的即可。

三、准备数据

[root@jiangnan ~]# vim sed.txt
[root@jiangnan ~]# cat sed.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

内部命令选项说明

文件内容增加操作,将数据追加到某个位置之后,使用命令 a 。

1. 在指定行后面追加内容

在第二到四行每行后新开一行追加数据: append data "haha"

[root@jiangnan data]# sed '2,4a\append data "haha"' sed.txt 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
append data "haha"
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

说明:a表示在匹配的行后面新开一行新增内容。`2,4a`表示第2到4行,如果不写数字,表示所有行。

2. 匹配字符串追加

找到包含"3 the"的行,在其后新开一行追加内容: append data "haha"

[root@jiangnan data]# sed '/3 the/a\append data "haha"' sed.txt 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

这里注意:/要匹配的字符串/

这种方法比较常用,匹配指定的字符串然后进行增加,比如XML文件等。

文件内容增加操作,将数据插入到某个位置之前,使用命令 i 。

3. 匹配字符串插入

找到包含"3 the"的行,在其前新开一行插入内容: insert data "haha"

[root@jiangnan data]# sed '/3 the/i\append data "haha"' sed.txt 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

这里注意:/要匹配的字符串/

使用方法同`a`类似。

文件内容修改操作—替换,将一行中匹配的内容替换为新的数据,使用命令s。

4. 在指定行后面追加内容

将sed.txt 中第二到第四行的dog替换为cat

[root@jiangnan data]# sed '2,4s/dog/cat/' sed.txt 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy cat.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

5. 匹配字符串所在行指定字符的替换

将包含字符串"3 the"的行中的dog替换为cat

[root@jiangnan data]# sed '/3 the/s/dog/cat/' sed.txt 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

这里注意:/匹配字符串所在的行/s/指定匹配字符串/要替换的字符串/

文件内容修改操作—更改,将一行中匹配的内容替换为新的数据,使用命令c。

6. 匹配行整行的替换

将sed.txt 文件中的第二、三、四行的内容更改为:change data "haha"

[root@jiangnan data]# sed '2,4c\change data haha' sed.txt 
1 the quick brown fox jumps over the lazy dog.
change data haha
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

7. 匹配字符串所在行整行的替换

将sed.txt文件中包含"3 the"的行内容更改为: change data "haha"

[root@jiangnan data]# sed '/3 the/c\change data haha' sed.txt 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
change data haha
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

文件内容修改操作—字符转换,将一行中匹配的内容替换为新的数据,使用命令y。

8. 将sed.txt中的a b c字符转换为对应的 A B C字符

[root@jiangnan data]# sed 'y/abc/ABC/' sed.txt 
1 the quiCk Brown fox jumps over the lAzy dog.
2 the quiCk Brown fox jumps over the lAzy dog.
3 the quiCk Brown fox jumps over the lAzy dog.
4 the quiCk Brown fox jumps over the lAzy dog.
5 the quiCk Brown fox jumps over the lAzy dog.
[root@jiangnan data]# 

文件内容删除,将文件中的指定数据删除,使用命令d。

9. 删除文件sed.txt 第三到第四行的数据

[root@jiangnan data]# sed '3,4d' sed.txt 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

文件内容查看,将文件内容输出到屏幕,使用命令p。

10. 打印sed.txt的内容

[root@jiangnan data]# sed 'p' sed.txt 
1 the quick brown fox jumps over the lazy dog.
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

11. 打印sed.txt文件包含字符串"3 the"的行

[root@jiangnan data]# sed '/3 the/p' sed.txt 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

现象:可以看得出,打印内容是重复的行,原因是打印了指定文件内容一次,又将读入缓存的所有数据打印了一次,所以会看到这样的效果, 如果不想看到这样的结果,可以加命令选项`-n`抑制内存输出即可。

[root@jiangnan data]# sed -n '/3 the/p' sed.txt 
3 the quick brown fox jumps over the lazy dog.
[root@jiangnan data]# 

0
博主关闭了所有页面的评论