Linux 三剑客之sed命令

71 阅读4分钟

1、sed介绍

    sed 全名为 stream editor,流编辑器,用程序的方式来编辑文本,功能相当的强大。是贝尔实验室的 Lee E.McMahon 在 1973 年到 1974 年之间开发完成,目前可以在大多数操作系统中使用,sed 的出现作为 grep 的继任者。与vim等编辑器不同,sed 是一种非交互式编辑器(即用户不必参与编辑过程),它使用预先设定好的编辑指令对输入的文本进行编辑,完成之后再输出编辑结构。sed 基本上就是在玩正则模式匹配,所以,玩sed的人,正则表达式一般都比较强。

2、sed工作原理

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

3、sed语法和常用选项

语法

sed [选项] ‘command’ 文件名称

image.png

image.png

image.png

4、案例

1、准备数据

[root@ecs-99217 ~]# cat -n test.txt
 1  My name is luhui.
 2  I teach linux.
 3  I like play computer game.
 4  My qq is 1205337324.
 5  My websit is http://python.cn.

2、输出文件第2、3行内容

注意sed 默认输出所有结果,-n 输出匹配的结果

[root@ecs-99217 ~]# sed '2,3p' test.txt
My name is luhui.
I teach linux.
I teach linux.
I like play computer game.
I like play computer game.
My qq is 1205337324.
My websit is http://python.cn.
[root@ecs-99217 ~]# sed -n  '2,3p' test.txt
I teach linux.
I like play computer game.

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

sed 可以实现grep过滤效果,必须把要过滤的内容放置到双斜线中

[root@ecs-99217 ~]# sed -n '/linux/p' test.txt
I teach linux.

4、删除含有game的行

[root@ecs-99217 ~]# sed '/game/d' test.txt
My name is luhui.
I teach linux.
My qq is 1205337324.
My websit is http://python.cn.
[root@ecs-99217 ~]# cat -n test.txt
     1  My name is luhui.
     2  I teach linux.
     3  I like play computer game.
     4  My qq is 1205337324.
     5  My websit is http://python.cn.

注意sed想要修改文件的内容,还得用 -i 参数,要不修改的都是内存值

[root@ecs-99217 ~]# sed '/game/d' test.txt -i
[root@ecs-99217 ~]# cat test.txt
My name is luhui.
I teach linux.
My qq is 1205337324.
My websit is http://python.cn.

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

.s内置符配合g,代表全局替换,中间的/可以替换为#@等

[root@ecs-99217 ~]# sed 's/My/His/g' test.txt
His name is luhui.
I teach linux.
His qq is 1205337324.
His websit is http://python.cn.

6、替换所有的My为His 同时,替换qq号为 8888888

[root@ecs-99217 ~]# sed -e 's/My/His/g' -e  's/1205337324/88888888/g' test.txt
His name is luhui.
I teach linux.
His qq is 88888888.
His websit is http://python.cn.

7、在文件第二行追加内容 a字符功能,写入到文件,还得添加 -i

[root@ecs-99217 ~]# sed -i '2a I am useing sed command' test.txt
[root@ecs-99217 ~]# cat test.txt
My name is luhui.
I teach linux.
I am useing sed command
My qq is 1205337324.
My websit is http://python.cn.

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

[root@ecs-99217 ~]# sed 'a --------------------' test.txt
My name is luhui.
--------------------
I teach linux.
--------------------
I am useing sed command
--------------------
I am useing sed command
--------------------
My qq is 1205337324.
--------------------
My websit is http://python.cn.
--------------------

9、在第二行上面插入

[root@ecs-99217 ~]# sed '2i I am 27' test.txt -i
[root@ecs-99217 ~]# cat test.txt
My name is luhui.
I am 27
I teach linux.
I am useing sed command
I am useing sed command
My qq is 1205337324.
My websit is http://python.cn.

10、取出linux 的ip 地址

image.png

[root@ecs-99217 ~]# ip a  show eth0| sed -n '3p'|sed 's/^.*inet//g'|sed 's/\/.*//g'
 192.168.0.190
[root@ecs-99217 ~]# ip a  show eth0| sed  -e '3s/^.*inet//' -e '3s/\/.*//p' -n
 192.168.0.190