Linux-----sed案例练习

1.数据准备

准备数据如下:

bash 复制代码
[root@openEuler ~]# cat openlab.txt 
My name is jock.
I teach linux.
I like play computer game.
My qq is 24523452
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com

2.题目

2.1、输出文件第2,3行的内容:

bash 复制代码
[root@openEuler ~]# sed -n '2,3p' openlab.txt 
I teach linux.
I like play computer game.

2.2、过滤出含有linux的字符串行:

bash 复制代码
[root@openEuler ~]# sed -n '/linux/p' openlab.txt 
I teach linux.

2.3、删除含有game的行:

bash 复制代码
[root@openEuler ~]# sed '/game/d' openlab.txt 
My name is jock.
I teach linux.
My qq is 24523452
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com

2.4、删除第5行到结尾:

bash 复制代码
[root@openEuler ~]# sed '5,$d' openlab.txt 
My name is jock.
I teach linux.
I like play computer game.
My qq is 24523452

2.5、将文件中的My全部替换为His:

bash 复制代码
[root@openEuler ~]# sed 's/My/His/g' openlab.txt 
His name is jock.
I teach linux.
I like play computer game.
His qq is 24523452
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com

2.6、替换所有My为His,同时换掉QQ号为88888888:

bash 复制代码
[root@openEuler ~]# sed -e 's/My/His/g' -e 's/24523452/88888888/g' openlab.txt 
His name is jock.
I teach linux.
I like play computer game.
His qq is 88888888
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com
His website is http://www.xianoupeng.com

2.7、在文件第二行追加内容( a 字符功能),写入到文件(-i):

bash 复制代码
[root@openEuler ~]# sed -i '2a I like liunx' openlab.txt 
[root@openEuler ~]# cat openlab.txt 
My name is jock.
I teach linux.
I like liunx
I like play computer game.
My qq is 24523452
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com

2.8、添加多行信息,用换行符"\n":

bash 复制代码
[root@openEuler ~]# sed -i '3a I like liunx very much!\nand you?' openlab.txt 
[root@openEuler ~]# cat openlab.txt 
My name is jock.
I teach linux.
I like liunx
I like liunx very much!
and you?
I like play computer game.
My qq is 24523452
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com

2.9、在每一行下面插入新内容:

bash 复制代码
[root@openEuler ~]# sed 'a ----------------------------------------' openlab.txt 
My name is jock.
----------------------------------------
I teach linux.
----------------------------------------
I like liunx
----------------------------------------
I like liunx very much!
----------------------------------------
and you?
----------------------------------------
I like play computer game.
----------------------------------------
My qq is 24523452
----------------------------------------
My website is http://www.xianoupeng.com
----------------------------------------
My website is http://www.xianoupeng.com
----------------------------------------
My website is http://www.xianoupeng.com
----------------------------------------
My website is http://www.xianoupeng.com
----------------------------------------
My website is http://www.xianoupeng.com
----------------------------------------
My website is http://www.xianoupeng.com
----------------------------------------

2.10、在第二行上面插入内容:

bash 复制代码
[root@openEuler ~]# sed -i '2i I am a boy.' openlab.txt 
[root@openEuler ~]# cat openlab.txt 
My name is jock.
I am a boy.
I teach linux.
I like liunx
I like liunx very much!
and you?
I like play computer game.
My qq is 24523452
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com
My website is http://www.xianoupeng.com

注意:sed对文件进行操作处理时,改变的只是内存中的文件数据,源文件的数据不会发生改变,如果想对文件内的内容进行处理则需要加上 -i 这个选项。

3.补充-----sed的工作流程

如图所示:

如上面的图片一样,sed工作时会一行一行的读取文件内容,每次读取一行放入sed模式空间,然后与匹配模式相匹配,不管匹配成功还是失败,都会输出到屏幕上。失败则是直接将原内容输出,成功则是输出编辑处理过的数据。默认的输出可以取消(使用-n 选项) 。

相关推荐
dessler37 分钟前
Docker-run命令详细讲解
linux·运维·后端·docker
群联云防护小杜1 小时前
如何给负载均衡平台做好安全防御
运维·服务器·网络·网络协议·安全·负载均衡
PyAIGCMaster1 小时前
ubuntu装P104驱动
linux·运维·ubuntu
奈何不吃鱼1 小时前
【Linux】ubuntu依赖安装的各种问题汇总
linux·运维·服务器
icy、泡芙1 小时前
T527-----音频调试
linux·驱动开发·音视频
aherhuo1 小时前
kubevirt网络
linux·云原生·容器·kubernetes
爱码小白1 小时前
网络编程(王铭东老师)笔记
服务器·网络·笔记
zzzhpzhpzzz1 小时前
Ubuntu如何查看硬件型号
linux·运维·ubuntu
蜜獾云1 小时前
linux firewalld 命令详解
linux·运维·服务器·网络·windows·网络安全·firewalld
陌北v12 小时前
Docker Compose 配置指南
运维·docker·容器·docker-compose