您的位置:首页 > 运维架构 > Shell

Shell编程之正则表达式三剑客——sed工具

2019-10-09 20:12 1051 查看

sed工具概述

擅长对数据行进行处理,sed是一种流编辑器,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。利用sed命令可以将数据行进行替换、删除、新增、选取等特定工作。

sed命令常见用法

sed [选项] '操作' 参数
sed [选项] -f scriptfile 参数

常见的sed命令选项主要包含以下几种

-e 或--expression=:表示用指定命令或者脚本来处理输入的文本文件。
-f 或--file=:表示用指定的脚本文件来处理输入的文本文件。
-h 或--help:显示帮助。
-n、--quiet 或 silent:表示仅显示处理后的结果。
-i:直接编辑文本文件。

“操作”用于指定对文件操作的动作行为,也就是 sed 的命令。通常情况下是采用的“[n1[,n2]]”操作参数的格式。n1、n2 是可选的,不一定会存在,代表选择进行操作的行数

a:增加,在当前行下面增加一行指定内容。
c:替换,将选定行替换为指定内容。
d:删除,删除选定的行。
i:插入,在选定行上面插入一行指定内容。
p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用。
s:替换,替换指定字符。
y:字符转换。

用法示例

1,输出符合条件的文本(p表达正常输出)

sed -n 'p' test.txt 输出所有内容

[root@localhost ~]# sed -n 'p' test.txt
the football
you are best boy
PI=3.1415926535897
a wood cross

hello.
the boy

sed -n '2p' test.txt 输出第2行的内容
sed -n '2,5p' test.txt 输出2到5行的内容
sed -n 'p;n' test.txt 输出所有奇数行的内容,n表示读入下一行资料
sed -n 'n;p' test.txt 输出所有偶数行的内容

[root@localhost ~]# sed -n 'p;n' test.txt
the football
PI=3.1415926535897
the boy
[root@localhost ~]# sed -n 'n;p' test.txt
you are best boy
a wood cross
hello.

sed -n '2,5{p;n}' test.txt 输出2到5行中的奇数行
此时的奇数行是原来的第二行,是按照选择的行数的奇偶数决定的

[root@localhost ~]# sed -n '2,5{p;n}' test.txt
you are best boy
a wood cross

以上是 sed 命令的基本用法,sed 命令结合正则表达式时,格式略有不同,正则表达式以“/”包围。例如,以下操作是 sed 命令与正则表达式结合使用的示例。

[root@localhost ~]# sed -n '/the/p' test.txt  输出包含the的行
the football
the boy

sed -n '4,/the/p' test.txt 输出从第四行到包含第一个the的行

[root@localhost ~]# sed -n '4,/the/p' test.txt
a wood cross

hello.
the boy

sed -n '/the/=' test.txt 输出包含the的所在行号

[root@localhost ~]# sed -n '/the/=' test.txt
1
7

sed -n '/^PI/p' test.txt 输出以PI开头的行
sed -n '/[0-9]$/p' test.txt 输出以数字为结尾的行
sed -n '/\<wood\>/p' test.txt 输出包含单词wood的行

[root@localhost ~]# sed -n '/^PI/p' test.txt
PI=3.1415926535897
[root@localhost ~]# sed -n '/[0-9]$/p' test.txt
PI=3.1415926535897
[root@localhost ~]# sed -n '/\<wood\>/p' test.txt
a wood cross

2,删除符合条件的文本(d)

下面的命令中nl命令用于计算文件的行数,结合该命令以更加直观查看到命令执行的结果

nl test.txt | sed '3d' 删除第三行
nl test.txt | sed '3,5d' 删除3到5行
nl test.txt | sed '/cross/d' 删除带有cross的行 不包含用!取反

[root@localhost ~]# nl test.txt | sed '3d'
1  the football
2  you are best boy
4  a wood cross

5  hello.
6  the boy
[root@localhost ~]# nl test.txt | sed '3,5d'
1  the football
2  you are best boy
5  hello.
6  the boy
[root@localhost ~]# nl test.txt | sed '/\<hello\>/d'
1  the football
2  you are best boy
3  PI=3.1415926535897
4  a wood cross

6  the boy

sed '/^[a-zA-Z]/d' test.txt 删除以字母开头的行
sed '/.$/d' test.txt 删除以.结尾的行
sed '/^$/d' test.txt 删除空格的行

[root@localhost ~]# sed '/^[a-zA-Z]/d' test.txt

[root@localhost ~]# sed '/\.$/d' test.txt
the football
you are best boy
PI=3.1415926535897
a wood cross

the boy
[root@localhost ~]# sed '/^$/d' test.txt
the football
you are best boy
PI=3.1415926535897
a wood cross
hello.
the boy

注意: 若是删除重复的空行,即连续的空行只保留一个, 执行“ sed –e ‘/^$/{n;/^$/d}’test.txt”命令即可实现。其效果与“cat -s test.txt”相同,n 表示读下一行数据。

3,替换符合条件的文本

s(字符串替换)
c(整行/整块替换)
y(字符转换)命令选项,常见的用法如下所示。

4,迁移符合条件的文本

H,复制到剪贴板;
g、G,将剪贴板中的数据覆盖/追加至指定行;
w,保存为文件;
r,读取指定文件;
a,追加指定内容。

5,使用脚本编辑文件

使用 sed 脚本,将多个编辑指令存放到文件中(每行一条编辑指令),通过“-f”选项来调用。

例如:sed '1,5{H;d};17G' test.txt //将第 1~5 行内容转移至第 17 行后

以上操作改用脚本文件方式

[root@localhost ~]# vi opt.list 1,5H
1,5d
17G
[root@localhost ~]# sed -f opt.list test.txt

6,sed直接操作文件示例

编写一个脚本,用来调整 vsftpd 服务配置:禁止匿名用户,但允许本地用户(也允许写入)。

谢谢阅读!!!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: