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