Skip to content
KARINKARIN
博客主页
项目总览
知识要点
捣鼓折腾
Git笔记
Excel修炼秘籍
github icon
  • 博客主页
    • 项目总览
          • Shell脚本笔记
            • Shell脚本介绍
              • Shell语法
                • Shell格式化输出
                  • Shell格式化输入
                    • Shell变量
                      • Shell数组
                        • Shell运算
                          • Shell流程控制---if判断语句
                            • Shell流程控制---for循环语句
                              • Shell流程控制---while循环语句
                                • Shell流程控制---until循环语句
                                  • Shell流程控制---case分支语句
                                    • Shell函数
                                      • Shell正则表达式
                                        • Shell对文本的搜索---grep命令
                                          • Shell对文件的操作---sed命令
                                            • 介绍
                                              • sed 命令
                                                • sed数据处理原理
                                                  • sed 命令格式
                                                  • sed 使用小技巧
                                                    • 实例说明
                                                      • sed 内部命令
                                                        • sed 命令标志
                                                          • sed 命令选项
                                                            • 补充说明
                                                          • Shell对输出流的处理---awk命令
                                                            • Shell监控脚本
                                                              • Shell脚本---lamp环境安装

                                                            Shell对文件的操作---sed命令

                                                            author iconkarincalendar icon2021年4月9日category icon
                                                            • 教程
                                                            tag icon
                                                            • shell
                                                            timer icon大约 7 分钟

                                                            此页内容
                                                            • 介绍
                                                            • sed 命令
                                                              • sed数据处理原理
                                                              • sed 命令格式
                                                            • sed 使用小技巧
                                                            • 实例说明
                                                              • sed 内部命令
                                                              • sed 命令标志
                                                              • sed 命令选项
                                                              • 补充说明

                                                            # 介绍

                                                            在 shell 脚本编写中,时常会用到对文件的相关操作,比如增加内容、修改内容、删除部分内容、查看部分内容等,但是上述的这些操作一般都是需要在文本编辑器中才能操作,常用的文本编辑器有:gedit、vim、nano 等,又是交互式文本编辑器,脚本无法自己独立完成必须有人参与才可以完成。如果这样的话又违背了我们编写脚本的目的(全部由机器来完成,减少人的工作压力,提升工作效率)。emm......如何才能让这些操作全部脚本自己就搞定,而不需要人的参与(即完全自动化),而且又能按照我们的脚本预案来完成呢?

                                                            为了解决上述问题,linux 为我们提供了一些命令,比如:perl、sed 等命令,下面我们就来学习一下 sed 命令。

                                                            # sed 命令

                                                            sed 命令是 linux 中提供的一个外部命令,它是一个行(流)编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它和文本编辑器有本质的区别。

                                                            区别:

                                                            • 文本编辑器:编辑对象是文件
                                                            • 行编辑器:编辑对象是文件中的行

                                                            也就是前者一次处理一个文本,而后者是一次处理一个文本中的一行。这个是我们应该弄清楚且必须牢记的,否则可能无法理解 sed 的运行原理和使用精髓。

                                                            # sed数据处理原理

                                                            数据处理

                                                            读取的数据在缓存中处理,然后默认输出到屏幕

                                                            # sed 命令格式

                                                            命令格式如下:

                                                            [root@localhost ~]# sed [选项] '{内部命令}[标志]' [文件]
                                                            选项:
                                                            	-e		:将脚本中指定的命令添加到处理输入时执行的命令中;一行中要有多个条件
                                                            	-f		:将文件中指定的命令添加到处理输入时的命令中
                                                            	-n		:抑制自动输出
                                                            	-i		:编辑文件内容
                                                            	-i.bak	:编辑文件内容同时创建.bak的备份文件
                                                            	-r		:使用扩展的正则表达式
                                                            	!		:取反(跟在模式条件后,与shell有所区别)
                                                            	
                                                            内部命令:	
                                                            	a	:在匹配后面添加
                                                            	i	:在匹配前面添加
                                                            	p	:打印
                                                            	d	:删除
                                                            	s	:查找替换
                                                            	c	:编辑更改
                                                            	y	:转换(	N	D	P	)
                                                            	
                                                            标志:
                                                            	数字:表示新文本替换模式
                                                            	g	:表示用新文本替换现有文本的全部实例
                                                            	p	:表示打印原始的内容
                                                            	w	:将替换的结果写入文件
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            9
                                                            10
                                                            11
                                                            12
                                                            13
                                                            14
                                                            15
                                                            16
                                                            17
                                                            18
                                                            19
                                                            20
                                                            21
                                                            22
                                                            23
                                                            24

                                                            # sed 使用小技巧

                                                            $= :统计文本有多少行

                                                            [root@localhost ~ ]# sed '=' date
                                                            1
                                                            hello world!
                                                            2
                                                            you can you Up
                                                            3
                                                            bye bye
                                                            [root@localhost ~ ]# sed -n '$=' date
                                                            3
                                                            [root@localhost ~ ]# 
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            9
                                                            10

                                                            # 实例说明

                                                            #cat查看文件内容
                                                            [root@localhost ~ ]# cat data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            3.the quick brown fox jumps over the lazy dog.dog
                                                            4.the quick brown fox jumps over the lazy dog.dog
                                                            5.the quick brown fox jumps over the lazy dog.dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7

                                                            # sed 内部命令

                                                            #每行后面追加
                                                            [root@localhost ~ ]# sed 'a\hello world' data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            3.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            4.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            5.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            9
                                                            10
                                                            11
                                                            12
                                                            #第3行后面追加
                                                            [root@localhost ~ ]# sed '3a\hello world' data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            3.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            4.the quick brown fox jumps over the lazy dog.dog
                                                            5.the quick brown fox jumps over the lazy dog.dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            #第2-4行之间每行后面追加
                                                            [root@localhost ~ ]# sed '2,4a\hello world' data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            3.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            4.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            5.the quick brown fox jumps over the lazy dog.dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            9
                                                            10
                                                            #匹配3.the的行后面追加
                                                            [root@localhost ~ ]# sed '/3.the/a\hello world' data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            3.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            4.the quick brown fox jumps over the lazy dog.dog
                                                            5.the quick brown fox jumps over the lazy dog.dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8

                                                            总结

                                                            [root@localhost ~ ]# sed 'i\hello world' data				#每行前面插入
                                                            [root@localhost ~ ]# sed '3i\hello world' data				#第3行前面插入
                                                            [root@localhost ~ ]# sed '2,4i\hello world' data			#第2-4行之间每行前面插入
                                                            [root@localhost ~ ]# sed '/3.the/i\hello world' data		#匹配3.the的行前面插入
                                                            [root@localhost ~ ]# sed ‘d’ data							#删除所有
                                                            [root@localhost ~ ]# sed ‘3d’ data							#删除第3行
                                                            [root@localhost ~ ]# sed ‘2,4d’ data						#删除第2-4行
                                                            [root@localhost ~ ]# sed ‘/3.the/d’ data					#删除匹配3.the的那一行
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            #每一行中第一次出现的dog替换成cat
                                                            [root@localhost ~ ]# sed 's/dog/cat/' data
                                                            1.the quick brown fox jumps over the lazy cat.dog
                                                            2.the quick brown fox jumps over the lazy cat.dog
                                                            3.the quick brown fox jumps over the lazy cat.dog
                                                            4.the quick brown fox jumps over the lazy cat
                                                            5.the quick brown fox jumps over the lazy cat
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            #第3行中第一次出现的dog替换成cat
                                                            [root@localhost ~ ]# sed '3s/dog/cat/' data
                                                            #第2-4行中第一次出现的dog替换成cat
                                                            [root@localhost ~ ]# sed '2,4s/dog/cat/' data
                                                            #匹配3.the那行第一次出现的dog替换成cat
                                                            [root@linux ~ ]# sed '3.the/s/dog/cat/' data
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            #每一行内容都更改为hello world
                                                            [root@localhost ~ ]# sed 'c\hello world' data
                                                            hello world
                                                            hello world
                                                            hello world
                                                            hello world
                                                            hello world
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            #第3行内容更改为hello world
                                                            [root@localhost ~ ]# sed '3c\hello world' data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            4.the quick brown fox jumps over the lazy dog
                                                            5.the quick brown fox jumps over the lazy dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            #第2-4行删掉,加入一行hello world
                                                            [root@localhost ~ ]# sed '2,4c\hello world' data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            5.the quick brown fox jumps over the lazy dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            #匹配3.the那行内容更改为hello world
                                                            [root@localhost ~ ]# sed '/3.the/c\hello world' data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            hello world
                                                            4.the quick brown fox jumps over the lazy dog
                                                            5.the quick brown fox jumps over the lazy dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            #转换abcdefg/ABCDEFG一一对应
                                                            [root@localhost ~ ]# sed 'y/abcdefg/ABCDEFG' data
                                                            1.thE quiCk Brown Fox jumps ovEr thE lAzy DoG.DoG
                                                            2.thE quiCk Brown Fox jumps ovEr thE lAzy DoG.DoG
                                                            
                                                            3.thE quiCk Brown Fox jumps ovEr thE lAzy DoG.DoG
                                                            4.thE quiCk Brown Fox jumps ovEr thE lAzy DoG
                                                            5.thE quiCk Brown Fox jumps ovEr thE lAzy DoG
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            #把每行再打印一次,即每行打印2次
                                                            [root@localhost ~ ]# sed 'p' data
                                                            #把第三行打印2次
                                                            [root@localhost ~ ]# sed '3p' data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            3.the quick brown fox jumps over the lazy dog.dog
                                                            3.the quick brown fox jumps over the lazy dog.dog
                                                            4.the quick brown fox jumps over the lazy dog
                                                            5.the quick brown fox jumps over the lazy dog
                                                            #把第2-4之间的行,每行打印2次
                                                            [root@localhost ~ ]# sed '2,4p' data
                                                            #把匹配3.the的那行,打印2次
                                                            [root@localhost ~ ]# sed '/3.the/p' data
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            9
                                                            10
                                                            11
                                                            12
                                                            13
                                                            14

                                                            # sed 命令标志

                                                            #把每行的所有dog都替换成cat
                                                            [root@localhost ~ ]# sed ‘s/dog/cat/g’ data
                                                            1.the quick brown fox jumps over the lazy cat.cat
                                                            2.the quick brown fox jumps over the lazy cat.cat
                                                            3.the quick brown fox jumps over the lazy cat.cat
                                                            4.the quick brown fox jumps over the lazy cat
                                                            5.the quick brown fox jumps over the lazy cat
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            #把每行的第2次出现的dog替换成cat
                                                            [root@localhost ~ ]# sed ‘s/dog/cat/2’ data
                                                            1.the quick brown fox jumps over the lazy dog.cat
                                                            2.the quick brown fox jumps over the lazy dog.cat
                                                            3.the quick brown fox jumps over the lazy dog.cat
                                                            4.the quick brown fox jumps over the lazy dog
                                                            5.the quick brown fox jumps over the lazy dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            #把第3行第1次出现的dog替换成cat并打印2次
                                                            [root@localhost ~ ]# sed ‘3s/dog/cat/p’ data
                                                            1.the quick brown fox jumps over the lazy dog.dog
                                                            2.the quick brown fox jumps over the lazy dog.dog
                                                            3.the quick brown fox jumps over the lazy cat.dog
                                                            3.the quick brown fox jumps over the lazy cat.dog
                                                            
                                                            4.the quick brown fox jumps over the lazy dog
                                                            5.the quick brown fox jumps over the lazy dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            9
                                                            #把修改内容部分另存为data.bk
                                                            [root@localhost ~ ]# sed ‘3s/dog/cat/w data.bk’ data
                                                            [root@localhost ~ ]# ls
                                                            data		data.bk
                                                            [root@localhost ~ ]# cat data.bk
                                                            3.the quick brown fox jumps over the lazy cat.dog
                                                            [root@localhost ~ ]# 
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7

                                                            # sed 命令选项

                                                            #不打印原本文本的内容
                                                            [root@localhost ~ ]# sed -n ‘3s/dog/cat/p’ data
                                                            3.the quick brown fox jumps over the lazy cat.dog
                                                            
                                                            1
                                                            2
                                                            3
                                                            #多条件替换
                                                            [root@localhost ~ ]# sed -e ‘s/brown/white/;5s/dog/cat/’ data
                                                            5.the quick white fox jumps over the lazy cat
                                                            [root@localhost ~ ]# vim cmd_op
                                                            5s/brown/white/
                                                            5s/dog/cat/
                                                            #文件内容当作命令
                                                            [root@localhost ~ ]# sed -f cmd_op data	
                                                            5.the quick white fox jumps over the lazy cat
                                                            #修改原文件,不可逆,慎用
                                                            [root@localhost ~ ]# sed -i ‘s/dog/cat/’ data
                                                            #修改原文件前先备份一个.bak的文件
                                                            [root@localhost ~ ]# sed -i.bak ‘s/dog/cat/’ data			
                                                            [root@localhost ~ ]# ls
                                                            data		data.bk		data.bak
                                                            [root@localhost ~ ]# 
                                                            
                                                            1
                                                            2
                                                            3
                                                            4
                                                            5
                                                            6
                                                            7
                                                            8
                                                            9
                                                            10
                                                            11
                                                            12
                                                            13
                                                            14
                                                            15
                                                            16

                                                            # 补充说明

                                                            除了sed -i 会修改原文件内容,且一但修改则不能取消; 其他参数命令不会修改原文件内容,只是修改缓存中的内容并打印输出。

                                                            edit icon编辑此页open in new window
                                                            上一页
                                                            Shell对文本的搜索---grep命令
                                                            下一页
                                                            Shell对输出流的处理---awk命令
                                                            MIT Licensed
                                                            Copyright © 2022 karin