Linux命令之sed命令详解

一、sed介绍

  sed 是一种新型的,非交互式的编辑器。它能执行与编辑器 vi 和 ex 相同的编辑任务。sed 编辑器没有提供交互式使用方式,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。 sed 编辑器没有破坏性,它不会修改文件,除非使用 shell 重定向来保存输出结果。默认情况下,所有的输出行都被打印到屏幕上。
  sed 编辑器逐行处理文件(或输入),并将输出结果发送到屏幕。 sed 的命令就是在 vi和 ed/ex 编辑器中见到的那些。 sed 把当前正在处理的行保存在一个临时缓存区中,这个缓存区称为模式空间或临时缓冲。sed 处理完模式空间中的行后(即在该行上执行 sed 命令后),就把改行发送到屏幕上(除非之前有命令删除这一行或取消打印操作)。 sed 每处理完输入文件的最后一行后, sed 便结束运行。 sed 把每一行都存在临时缓存区中,对这个副本进行编辑,所以不会修改或破坏源文件。如图 1: sed 处理过程。
Linux命令之sed命令详解_第1张图片

二、sed命令使用示例

为了演示sed命令,临时创建test.txt文件,内容如下。

(base) [root@test1 ~]# ll /usr/ > test.txt
(base) [root@test1 ~]# cat test.txt
total 272
dr-xr-xr-x. 2 root root 49152 Jan 17 05:09 bin
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games
drwxr-xr-x. 40 root root 8192 Dec 15 22:02 include
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 lib
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 lib64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
dr-xr-xr-x. 2 root root 20480 Jan 16 06:56 sbin
drwxr-xr-x. 235 root root 8192 Jan 16 06:56 share
drwxr-xr-x. 4 root root 34 Dec 15 21:59 src
lrwxrwxrwx. 1 root root 10 Dec 15 21:59 tmp -> …/var/tmp

1、打印匹配的行

(base) [root@test1 ~]# sed -n ‘/lib/p’ test.txt
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 lib
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 lib64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libexec

2、替换指定的行

(base) [root@test1 ~]# sed ‘s/include/test2/g’ test.txt
total 272
dr-xr-xr-x. 2 root root 49152 Jan 17 05:09 bin
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games
drwxr-xr-x. 40 root root 8192 Dec 15 22:02 test2
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 lib
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 lib64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
dr-xr-xr-x. 2 root root 20480 Jan 16 06:56 sbin
drwxr-xr-x. 235 root root 8192 Jan 16 06:56 share
drwxr-xr-x. 4 root root 34 Dec 15 21:59 src
lrwxrwxrwx. 1 root root 10 Dec 15 21:59 tmp -> …/var/tmp

3、删除指定行

删除的行号含,例如‘10,13d’删除了第10号至第13行。

(base) [root@test1 ~]# sed ‘10,13d’ test.txt
total 272
dr-xr-xr-x. 2 root root 49152 Jan 17 05:09 bin
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games
drwxr-xr-x. 40 root root 8192 Dec 15 22:02 include
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 lib
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 lib64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
(base) [root@test1 ~]# sed ‘5,$d’ test.txt
total 272
dr-xr-xr-x. 2 root root 49152 Jan 17 05:09 bin
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games

4、匹配字符串的行行尾添加指定内容

(base) [root@test1 ~]# sed ‘/^dr-/ s/$/可读目录/’ test.txt
total 272
dr-xr-xr-x. 2 root root 49152 Jan 17 05:09 bin可读目录
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games
drwxr-xr-x. 40 root root 8192 Dec 15 22:02 include
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 lib可读目录
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 lib64可读目录
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
dr-xr-xr-x. 2 root root 20480 Jan 16 06:56 sbin可读目录
drwxr-xr-x. 235 root root 8192 Jan 16 06:56 share
drwxr-xr-x. 4 root root 34 Dec 15 21:59 src
lrwxrwxrwx. 1 root root 10 Dec 15 21:59 tmp -> …/var/tmp

5、匹配字符串的位置后添加指定内容

(base) [root@test1 ~]# sed ‘s/^dr-/&可读目录/’ test.txt
total 272
dr-可读目录xr-xr-x. 2 root root 49152 Jan 17 05:09 bin
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games
drwxr-xr-x. 40 root root 8192 Dec 15 22:02 include
dr-可读目录xr-xr-x. 43 root root 4096 Dec 15 22:03 lib
dr-可读目录xr-xr-x. 143 root root 81920 Jan 16 06:56 lib64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
dr-可读目录xr-xr-x. 2 root root 20480 Jan 16 06:56 sbin
drwxr-xr-x. 235 root root 8192 Jan 16 06:56 share
drwxr-xr-x. 4 root root 34 Dec 15 21:59 src
lrwxrwxrwx. 1 root root 10 Dec 15 21:59 tmp -> …/var/tmp

6、打印指定行开始开始匹配字符串的之间的行

(base) [root@test1 ~]# sed -n ‘5,/drwxr/p’ test.txt
Linux命令之sed命令详解_第2张图片

7、多重编辑

(base) [root@test1 ~]# sed -e ‘1,5d’ -e ‘s/sbin/stestttttttt/g’ test.txt
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 lib
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 lib64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
dr-xr-xr-x. 2 root root 20480 Jan 16 06:56 stestttttttt
drwxr-xr-x. 235 root root 8192 Jan 16 06:56 share
drwxr-xr-x. 4 root root 34 Dec 15 21:59 src
lrwxrwxrwx. 1 root root 10 Dec 15 21:59 tmp -> …/var/tmp

8、内容追加

(base) [root@test1 ~]# sed ‘/^lrw/a this is add something!’ test.txt
total 272
dr-xr-xr-x. 2 root root 49152 Jan 17 05:09 bin
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games
drwxr-xr-x. 40 root root 8192 Dec 15 22:02 include
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 lib
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 lib64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
dr-xr-xr-x. 2 root root 20480 Jan 16 06:56 sbin
drwxr-xr-x. 235 root root 8192 Jan 16 06:56 share
drwxr-xr-x. 4 root root 34 Dec 15 21:59 src
lrwxrwxrwx. 1 root root 10 Dec 15 21:59 tmp -> …/var/tmp
this is add something!

9、直接修改当前文档

使用-i参数直接更新当前文档。

(base) [root@test1 ~]# sed -i ‘s/lib/libbbbbbbb/g’ test.txt
(base) [root@test1 ~]# cat test.txt
total 272
dr-xr-xr-x. 2 root root 49152 Jan 17 05:09 bin
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games
drwxr-xr-x. 40 root root 8192 Dec 15 22:02 include
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 libbbbbbbb
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 libbbbbbbb64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libbbbbbbbexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
dr-xr-xr-x. 2 root root 20480 Jan 16 06:56 sbin
drwxr-xr-x. 235 root root 8192 Jan 16 06:56 share
drwxr-xr-x. 4 root root 34 Dec 15 21:59 src
lrwxrwxrwx. 1 root root 10 Dec 15 21:59 tmp -> …/var/tmp

10、直接修改匹配行为指定内容

(base) [root@test1 ~]# sed ‘/src$/c this is src’ test.txt
total 272
dr-xr-xr-x. 2 root root 49152 Jan 17 05:09 bin
drwxr-xr-x. 2 root root 6 Apr 10 2018 etc
drwxr-xr-x. 2 root root 6 Apr 10 2018 games
drwxr-xr-x. 40 root root 8192 Dec 15 22:02 include
dr-xr-xr-x. 43 root root 4096 Dec 15 22:03 libbbbbbbb
dr-xr-xr-x. 143 root root 81920 Jan 16 06:56 libbbbbbbb64
drwxr-xr-x. 49 root root 12288 Dec 15 22:03 libbbbbbbbexec
drwxr-xr-x. 12 root root 131 Dec 15 21:59 local
dr-xr-xr-x. 2 root root 20480 Jan 16 06:56 sbin
drwxr-xr-x. 235 root root 8192 Jan 16 06:56 share
this is src
lrwxrwxrwx. 1 root root 10 Dec 15 21:59 tmp -> …/var/tmp

11、字符转换

  字符按照一对一的方式从左到右进行转换。例如 y/abc/ABC/,会把小写字母转换成大写字母, a–>A,b–>B,c–>C。

(base) [root@test1 ~]# sed ‘1,13y/drwx/DRWX/’ test.txt
total 272
DR-XR-XR-X. 2 Root Root 49152 Jan 17 05:09 bin
DRWXR-XR-X. 2 Root Root 6 ApR 10 2018 etc
DRWXR-XR-X. 2 Root Root 6 ApR 10 2018 games
DRWXR-XR-X. 40 Root Root 8192 Dec 15 22:02 incluDe
DR-XR-XR-X. 43 Root Root 4096 Dec 15 22:03 libbbbbbbb
DR-XR-XR-X. 143 Root Root 81920 Jan 16 06:56 libbbbbbbb64
DRWXR-XR-X. 49 Root Root 12288 Dec 15 22:03 libbbbbbbbeXec
DRWXR-XR-X. 12 Root Root 131 Dec 15 21:59 local
DR-XR-XR-X. 2 Root Root 20480 Jan 16 06:56 sbin
DRWXR-XR-X. 235 Root Root 8192 Jan 16 06:56 shaRe
DRWXR-XR-X. 4 Root Root 34 Dec 15 21:59 sRc
lRWXRWXRWX. 1 Root Root 10 Dec 15 21:59 tmp -> …/vaR/tmp

三、sed命令参数详解

1、sed 命令的基本格式

#sed [选项] [脚本命令] 文件名
(base) [root@test1 ~]# sed --help
Usage: sed [OPTION]… {script-only-if-no-other-script} [input-file]…

2、参数说明

  • -e

你可能感兴趣的:(linux命令集锦,sed,文档编辑,shell命令,逐行处理,Linux)