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 选项) 。

相关推荐
九河云2 小时前
AWS账号注册费用详解:新用户是否需要付费?
服务器·云计算·aws
Lary_Rock2 小时前
RK3576 LINUX RKNN SDK 测试
linux·运维·服务器
幺零九零零3 小时前
【计算机网络】TCP协议面试常考(一)
服务器·tcp/ip·计算机网络
云飞云共享云桌面4 小时前
8位机械工程师如何共享一台图形工作站算力?
linux·服务器·网络
Peter_chq4 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
一坨阿亮5 小时前
Linux 使用中的问题
linux·运维
dsywws6 小时前
Linux学习笔记之vim入门
linux·笔记·学习
幺零九零零7 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
wclass-zhengge7 小时前
Docker篇(Docker Compose)
运维·docker·容器
李启柱7 小时前
项目开发流程规范文档
运维·软件构建·个人开发·设计规范