shell 编程之正则表达式与文本处理器
这里写目录标题
- 正则表达式
- 正则表达式的定义
- 正则表达式用途
- 基础正则表达式
- 基础正则表达式示例
- 查找特定字符
- 利用中括号“[ ]”来查找集合字符
- 查找行首“^”与行尾字符“$”
- 查找任意一个字符“.”与重复字符“*”
- 查找连续字符范围“{}”
- 查询两个0的字符
- 查询以 w 开头以 d 结尾,中间包含 2~5 个0的字符串
- 元字符总结
- 扩展正则表达式z
- 文本处理器
- sed 工具
- 输出符合条件的文本(p 表示正常输出)
- 删除符合条件的文本(d)
- 替换符合条件的文本
- 迁移符合条件的文本
- 使用脚本编辑文件
- 使用脚本编辑文件
- sed 直接操作文件示例
- awk 工具
- 按行输出文本
- 按字段输出文本
- 通过管道、双引号调用 shell命令
正则表达式
正则表达式的定义
正则表达式又称正规表达式、常规表达式。在代码中常简写为regex、regexp 或 RE。正则表达式是使用单个字符串来描述、匹配一系列符合某个句法规则的字符串,简单来说,是一种匹配字符串的方法通过一些特殊符号,实现快速查找、删除、替换某个特定字符串
正则表达式是由普通字符与元字符组成的文字模式。模式用于描述在搜索文本时要匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。其中普通字符包括大小写字母、数字、标点符号及一些其他符号,元字符则是指那些在正则表达式中具有特殊意义的专用字符,可以用来规定其前导字符(即位于元字符前面的字符)在目标对象中的出现模式
正则表达式一般用于脚本编程与文本编辑器中。很多文本处理器与程序设计语言均文持正则表达式,例如 Linux 系统中常见的文本处理器(grep、egrep、sed、awk)以及应用比较广泛的 Python 语言。正则表达式具备很强大的文本匹配功能,能够在文本海洋中快速高效地处理文本
正则表达式用途
对于一般计算机用户来说,由于使用到正则表达式的机会不多,所以无法体会正则表达式的魅力,而对于系统管理员来说,正则表达式则是必备技能之一
正则表达式对于系统管理员来说是非常重要的,系统运行过程中会产生大量的信息,这些信息有些是非常重要的,有些则仅是告知的信息。身为系统管理员如果直接看这么多的信息数据,无法快速定位到重要的信息,如“用户账号登录失败”“服务启动失败”等信息。这时可以通过正则表达式快速提取“有问题”的信息。如此一来,可以将运维工作变得更加简单、方便
目前很多软件也支持正则表达式,最常见的就是邮件服务器。在Internet中,垃圾/广告邮件经常会造成网络塞车,如果在服务器端就将这些问题邮件提前剔除的话,客户端就会减少很多不必要的带宽消耗。而目前常用的邮件服务器 postfix以及支持邮件服务器的相关分析软件都支持正则表达式的对比功能。将来信的标题、内容与特殊字符串进行对比,发现问题邮件就过滤掉
除邮件服务器之外,很多服务器软件都支持正则表达式。虽然这些软件都支持正则表达式,不过字符串的对比规则还需要系统管理员来添加,因此正则表达式是系统管理员必须掌握的技能之一
基础正则表达式
正则表达式的字符串表达方法根据不同的严谨程度与功能分为基本正则表达式与扩展正则表达式。基础正则表达式是常用正则表达式最基础的部分。在Linux 系统中常见的文件处理工具中grep 与 sed支持基础正则表达式,而 egrep与 awk 支持扩展正则表达式。掌握基础正则表达式的使用方法,首先必须了解基本正则表达式所包含元字符的含义,下面通过 grep 命令以举例的方式逐个介绍
基础正则表达式示例
[root@localhost ~]# vim zz.txt
[root@localhost ~]# cat zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit. PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
查找特定字符
查找特定字符非常简单,如执行以下命令即可从 zz.txt 文件中查找出特定字符“the”所在位置。其中“-n”表示显示行号、“-ì”表示不区分大小写。命令执行后,符合匹配标准的字符,字z体颜色会变为红色(本章中全部通过加粗显示代替)
[root@localhost ~]# grep -n 'the' zz.txt
3:the tongue is boneless but it breaks bones.12!
4:google is the best tools for search keyword. The year ahead will test our political
5:estavlishment to the limit. PI=3.141592653589793238462643383249901429
若反向选择,如查找不包含“the”字符的行,则需要通过 grep 命令的“-v”选项实现,并配合“-n”一起使用显示行号
[root@localhost ~]# grep -vn 'the' zz.txt
1:he was short and fat. He was wearing a blue polo shirt with black pants. The home
2:of Football on BBC Sport online.
6:a wood cross!
7:Actions speak louder than words
8:#woood #
9:#wooooood #
10:AxyzxyzxyzxyC
11:I bet this place is really spooky late at night!
12:Misfortunes never come alone/single.
13:I shouldn't have lett so tast.
利用中括号“[ ]”来查找集合字符
想要査找“shirt”与“short”这两个字符串时,可以发现这两个字符串均包含“sh”与“rt”。此时执行以下命令即可同时査找到“shirt”与“short”这两个字符串,其中“[ ]”中无论有几个字符,都仅代表一个字符,也就是说“[io]”表示匹配“i”或者“o”
[root@localhost ~]# grep -n 'sh[io]rt' zz.txt
1:he was short and fat. He was wearing a blue polo shirt with black pants. The home
若要查找包含重复单个字符“oo”时,只需要执行以下命令即可
[root@localhost ~]# grep -n 'oo' zz.txt
2:of Football on BBC Sport online.
4:google is the best tools for search keyword. The year ahead will test our political
6:a wood cross!
8:#woood #
9:#wooooood #
11:I bet this place is really spooky late at night!
若查找“oo”前面不是“w”的字符串,只需要通过集合字符的反向选择“ [ ^ ]"来实现该目的。例如执行“grep -n’[ ^w ]oo’zz.txt”命令表示在zz.txt 文本中査找“oo”前面不是“w”的字符串
[root@localhost ~]# grep -n '[^w]oo' zz.txt
2:of Football on BBC Sport online.
4:google is the best tools for search keyword. The year ahead will test our political
8:#woood #
9:#wooooood #
11:I bet this place is really spooky late at night!
在上述命令的执行结果中发现“woood”与“wooooood”也符合匹配规则,二者均包含“w”。其实通过执行结果就可以看出,符合匹配标准的字符加粗显示,而上述结果中可以得知,“#woood #”中加粗显示的是“ooo”,而“oo”前面的“o”是符合匹配规则的。同理“#woooooood #”也符合匹配规则。
若不希望“oo”前面存在小写字母,可以使用“grep -n’ [ ^ a-z]oo’zz.txt”命令实现,其中 a-z”表示小写字母,大写字母则通过“A-Z”表示
[root@localhost ~]# grep -n '[^a-z]oo' zz.txt
2:of Football on BBC Sport online.
查找包含数字的行可以通过“grep -n’[0-9]zz.txt”命令来实现
[root@localhost ~]# grep -n '[0-9]' zz.txt
3:the tongue is boneless but it breaks bones.12!
5:estavlishment to the limit. PI=3.141592653589793238462643383249901429
查找行首“^”与行尾字符“$”
基础正则表达式包含两个定位元字符:“ ^ ”(行首)与“$”(行尾)。在上面的示例中,査询“the”字符串时出现了很多包含“the”的行,如果想要査询以“the”字符串为行首的行,则可以通过“^”元字符来实现
[root@localhost ~]# grep -n '^the' zz.txt
3:the tongue is boneless but it breaks bones.12!
查询以小写字母开头的行可以通过“ ^ [a-z]”规则来过滤,查询大写字母开头的行则使用“^ [A-Z]”规则,若查询不以字母开头的行则使用“^ [ ^a-zA-Z]”规则
[root@localhost ~]# grep -n '^[a-z]' zz.txt
1:he was short and fat. He was wearing a blue polo shirt with black pants. The home
2:of Football on BBC Sport online.
3:the tongue is boneless but it breaks bones.12!
4:google is the best tools for search keyword. The year ahead will test our political
5:estavlishment to the limit. PI=3.141592653589793238462643383249901429
6:a wood cross!
[root@localhost ~]# grep -n '^[A-Z]' zz.txt
7:Actions speak louder than words
10:AxyzxyzxyzxyC
11:I bet this place is really spooky late at night!
12:Misfortunes never come alone/single.
13:I shouldn't have lett so tast.
[root@localhost ~]# grep -n '^[^a-zA-Z]' zz.txt
8:#woood #
9:#wooooood #
“^”符号在元字符集合“[]”符号内外的作用是不一样的,在“[]”符号内表示反向选择,在“[]”符号外则代表定位行首。反之,若想查找以某一特定字符结尾的行则可以使用“$”定位符。例如,执行以下命令即可实现査询以小数点(.)结尾的行。因为小数点(.)在正则表达式中也是一个元字符(后面会讲到),所以在这里需要用转义字符“\”将具有特殊意义的字符转化成普通字符
[root@localhost ~]# grep -n '\.$' zz.txt
2:of Football on BBC Sport online.
12:Misfortunes never come alone/single.
13:I shouldn't have lett so tast.
当查询空白行时,执行“grep-n’^$'test.txt”命令即可
[root@localhost ~]# grep -n '^$' zz.txt
8:
查找任意一个字符“.”与重复字符“*”
前面提到,在正则表达式中小数点(.)也是一个元字符,代表任意一个字符。例如执行以下命令就可以査找“w??d”的字符串,即共有四个字符,以 w开头 d 结尾
[root@localhost ~]# grep -n 'w..d' zz.txt
4:google is the best tools for search keyword. The year ahead will test our political
6:a wood cross!
7:Actions speak louder than words
在上述结果中,“wood”字符串“w…d”匹配规则。若想要査询 00、000、o0000 等资料,则需要使用星号()元字符。但需要注意的是,““”代表的是重复零个或多个前面的单字符。“o*”表示拥有零个(即为空字符)或大于等于一个“o”的字符,因为允许空字符,所以执行“grep-n’o*’ zz.txt”命令会将文本中所有的内容都输出打印。如果是“oo*”,则第一个 。必须存在,第二个 。 则是零个或多个 o,所以凡是包含 0、00、000、000,等的资料都符合标准。同理,若查询包含至少两个 。 以上的字符串,则执行“grep -n ‘ooo*’ zz.txt”命令即可
[root@localhost ~]# grep -n 'ooo*' zz.txt
2:of Football on BBC Sport online.
4:google is the best tools for search keyword. The year ahead will test our political
6:a wood cross!
9:#woood #
10:#wooooood #
12:I bet this place is really spooky late at night!
查询以 w开头 d 结尾,中间包含至少一个。的字符串,执行以下命令即可实现
[root@localhost ~]# grep -n 'woo*d' zz.txt
6:a wood cross!
9:#woood #
10:#wooooood #
执行以下命令即可查询以 w开头 d结尾,中间的字符可有可无的字符串
[root@localhost ~]# grep -n 'w.*d' zz.txt
1:he was short and fat. He was wearing a blue polo shirt with black pants. The home
4:google is the best tools for search keyword. The year ahead will test our political
6:a wood cross!
7:Actions speak louder than words
9:#woood #
10:#wooooood #
执行以下命令即可查询任意数字所在行
[root@localhost ~]# grep -n '[0-9][0-9]*' zz.txt
3:the tongue is boneless but it breaks bones.12!
5:estavlishment to the limit. PI=3.141592653589793238462643383249901429
查找连续字符范围“{}”
在上面的示例中,使用了“.”与“*”来设定零个到无限多个重复的字符,如果想要限制一个范围内的重复的字符串该如何实现呢?例如,查找三到五个0的连续字符,这个时候就需要使用基础正则表达式中的限定范围的字符“{}”。因为“{}”在 Shell 中具有特殊意义,所以在使用“{}”字符时,需要利用转义字符“\”,将“{}”字符转换成普通字符
查询两个0的字符
[root@localhost ~]# grep -n 'o\{2\}' zz.txt
2:of Football on BBC Sport online.
4:google is the best tools for search keyword. The year ahead will test our political
6:a wood cross!
9:#woood #
10:#wooooood #
12:I bet this place is really spooky late at night!
查询以 w 开头以 d 结尾,中间包含 2~5 个0的字符串
[root@localhost ~]# grep -n 'wo\{2,5\}d' zz.txt
6:a wood cross!
9:#woood #
查询以 w 开头以 d 结尾,中间包含 2 个或 2 个以上0的字符串
[root@localhost ~]# grep -n 'wo\{2,\}d' zz.txt
6:a wood cross!
9:#woood #
10:#wooooood #
元字符总结
通过上面几个简单的示例,可以了解到常见的基础正则表达式的元字符主要包括以下几个
字符 | 说明 |
---|---|
\ | 将下一个字符标记为一个特殊字符、或一个原义字符、或一个 向后引用、或一个八进制转义符 |
^ | 匹配输入字符串的开始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的子表达式零次或多次 |
+ | 匹配前面的子表达式一次或多次 |
? | 匹配前面的子表达式零次或一次 |
. | 匹配除换行符(\n、\r)之外的任何单个字符 |
[a-z] | 字符范围。匹配指定范围内的任意字符 |
{n} | n是一个非负整数,匹配确定的n次 |
{n.} | n 是一个非负整数,至少匹配n 次 |
{n.m} | m 和 n均为非负整数,其中n<=m。最少匹配n次且最多匹配m次 |
\d | 匹配一个数字字符。等价于 [0-9] |
\D | 匹配一个非数字字符。等价于[ ^0-9] |
\s | 匹配任何空白字符,包括空格、 制表符、换页符等等。等价于[\fn\r\t\v] |
\S | 匹配任何非空白字符。等价于[ ^\An\rt\v] |
\w | 匹配字母、数字、下划线。等价于’[A-Za-z0-9-]’ |
\W | 匹配非字母、数字、下划线。等价于’[ ^A-Za-z0-9_]’ |
\n | 匹配一个换行符 |
\f | 匹配一个换页符 |
\r | 匹配一个回车符 |
扩展正则表达式z
通常情况下会使用基础正则表达式就已经足够了,但有时为了简化整个指令,需要使用范围更广的扩展正则表达式。例如,使用基础正则表达式査询除文件中空白行与行首为“#”之外的行(通常用于查看生效的配置文件),执行“grep-v’ ^ $’zz.txt|grep -v’^#’”即可实现。这里需要使用管道命令来搜索两次。如果使用扩展正则表达式,可以简化为“egrep-v’ ^ $ | ^#’zz.txt”,其中,单引号内的管道符号表示或者(or)
此外,grep 命令仅支持基础正则表达式,如果使用扩展正则表达式,需要使用 egrep 或 awk 命令。awk 命令在后面的小节进行讲解,这里我们直接使用 egrep 命令。egrep 命令与 grep 命令的用法基本相似。egrep 命令是一个搜索文件获得模式,使用该命令可以搜索文件中的任意字符串和符号,也可以搜索一个或多个文件的字符串,一个提示符可以是单个字符、一个字符串、一个字或一个句子
与基础正则表达式类型相同,扩展正则表达式也包含多个元字符,常见的扩展正则表达式的元字符主要包括以下几个
元字符 | 作用与示例 |
---|---|
+ | z作用:重复一个或者一个以上的前一个字符 示例:执行“egrep -n’wo+d’ zz.txt"命令,即可査询"wood"“woood”"woo000ood"等字符串 |
? | 作用:零个或者一个的前一个字符 示例:执行“egrep -n’bes?t’ zz.txt"命令,即可査询“bet"“best"这两个字符串 |
I | 作用:使用或者(or)的方式找出多个字符 示例:执行“egrep -n’ofis |
( ) | 作用:查找“组”字符串 示例:“egrep -n 't(a |
( )+ | 作用:辨别多个重复的组 示例:“egrep -n’A(xyz)+C zz.tx"。该命令是査询开头的"A"结尾是"C",中间有一个以上的"xyz"字符串的意思 |
文本处理器
在 Linux/NIX 系统中包含很多种类的文本处理器或文本编辑器,其中包括我们之前学习过的 VIM编辑器与 grep 等。而 grep,sed,awk 更是 Shell 编程中经常用到的文本处理工具,被称之为 Shell编程三剑客
sed 工具
sed(stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。sed 也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于 shel1 脚本中,用以完成各种自动化处理任务
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 是可选的,代表选择进行操作的行数,如操作需要在 5~20 行之间进行,则表示为“5,20 动作行为”
- a:增加,在当前行下面增加一行指定内容
- C:替换,将选定行替换为指定内容
- d:删除,删除选定的行
- i:插入,在选定行上面插入一行指定内容
- p:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用
- S:替换,替换指定字符
- y:字符转换
输出符合条件的文本(p 表示正常输出)
输出所有内容,等同于 cat zz.txt
[root@localhost ~]# sed -n 'p' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit. PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
输出第 3 行
[root@localhost ~]# sed -n '3p' zz.txt
the tongue is boneless but it breaks bones.12!
输出 3~5 行
[root@localhost ~]# sed -n '3,5p' zz.txt
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit. PI=3.141592653589793238462643383249901429
输出所有奇数行,n 表示读入下一行资料
[root@localhost ~]# sed -n 'p;n' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
the tongue is boneless but it breaks bones.12!
estavlishment to the limit. PI=3.141592653589793238462643383249901429
Actions speak louder than words
#woood #
AxyzxyzxyzxyC
Misfortunes never come alone/single.
输出所有偶数行,n 表示读入下一行资料
[root@localhost ~]# sed -n 'n;p' zz.txt
of Football on BBC Sport online.
google is the best tools for search keyword. The year ahead will test our political
a wood cross!#wooooood #
I bet this place is really spooky late at night!
I shouldn't have lett so tast.
输出第 1~5 行之间的奇数行(第1、3、 5 行)
[root@localhost ~]# sed -n '1,5{p;n}' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
the tongue is boneless but it breaks bones.12!
estavlishment to the limit. PI=3.141592653589793238462643383249901429
输出第 10 行至文件尾之间的偶数行
[root@localhost ~]# sed -n '10,${n;p}' zz.txt
AxyzxyzxyzxyC
Misfortunes never come alone/single.
在执行“sed -n’10,${n;p}'zz.txt”命令时,读取的第 1 行是文件的第 10 行,读取的第 2行是文件的第 11 行,依此类推,所以输出的偶数行是文件的第 11 行、13 行直至文件结尾,其中包括空行
以上是 sed 命令的基本用法,sed 命令结合正则表达式时,格式略有不同,正则表达式以“/”包围
输出包含 the 的行
[root@localhost ~]# sed -n '/the/p' zz.txt
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit. PI=3.141592653589793238462643383249901429
输出从第 4 行至第一个包含 the 的行
[root@localhost ~]# sed -n '4,/the/p' zz.txt
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit. PI=3.141592653589793238462643383249901429
输出包含 the 的行所在的行号,等号(=)用来输出行号
[root@localhost ~]# sed -n '/the/=' zz.txt
3
4
5
输出以 PI 开头的行
[root@localhost ~]# sed -n '/^PI/p' zz.txt
PI=3.141592653589793238462643383249901429
输出以数字结尾的行
[root@localhost ~]# sed -n '/[0-9]$/p' zz.txt
PI=3.141592653589793238462643383249901429
输出包含单词 wood 的行,<、>代表单词边界
[root@localhost ~]# sed -n '/\<wood\>/p' zz.txt
a wood cross!
删除符合条件的文本(d)
因为后面的示例还需要使用测试文件 zz.txt,所以在执行删除操作之前需要先将测试文件备份
删除第三行
[root@localhost ~]# nl zz.txt | sed '3d'1 he was short and fat. He was wearing a blue polo shirt with black pants. The home2 of Football on BBC Sport online.4 google is the best tools for search keyword. The year ahead will test our political5 estavlishment to the limit.6 PI=3.1415926535897932384626433832499014297 a wood cross!8 Actions speak louder than words9 #woood #10 #wooooood #11 AxyzxyzxyzxyC12 I bet this place is really spooky late at night!13 Misfortunes never come alone/single.14 I shouldn't have lett so tast.
删除3~5行
[root@localhost ~]# nl zz.txt | sed '3,5d'1 he was short and fat. He was wearing a blue polo shirt with black pants. The home2 of Football on BBC Sport online.6 PI=3.1415926535897932384626433832499014297 a wood cross!8 Actions speak louder than words9 #woood #10 #wooooood #11 AxyzxyzxyzxyC12 I bet this place is really spooky late at night!13 Misfortunes never come alone/single.14 I shouldn't have lett so tast.
删除包含 cross 的行,原本的第 8 行被删除;如果要删除不包含 cross 的行,用!符号表示取反操作,如’/cross/!d’……
[root@localhost ~]# nl zz.txt | sed '/cross/d'1 he was short and fat. He was wearing a blue polo shirt with black pants. The home2 of Football on BBC Sport online.3 the tongue is boneless but it breaks bones.12!4 google is the best tools for search keyword. The year ahead will test our political5 estavlishment to the limit.6 PI=3.1415926535897932384626433832499014298 Actions speak louder than words9 #woood #10 #wooooood #11 AxyzxyzxyzxyC12 I bet this place is really spooky late at night!13 Misfortunes never come alone/single.14 I shouldn't have lett so tast.
删除以小写字母开头的行
[root@localhost ~]# sed '/^[a-z]/d' zz.txt
PI=3.141592653589793238462643383249901429
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
删除以"."结尾的行
[root@localhost ~]# sed '/\.$/d' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
删除所有空行
[root@localhost ~]# sed '/^$/d' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
注意:若是删除重复的空行,即连续的空行只保留一个,执行“sed -e’/^ $ /{n;/^$/d}'zz.txt”命令即可实现。其效果与“cat-s zz.txt”相同,n 表示读下一行数据
替换符合条件的文本
在使用 sed 命令进行替换操作时需要用到 s(字符串替换)、c(整行/整块替换)、y(字符转换)命令选项
将每行中的第一个 the 替换为 THE
[root@localhost ~]# sed 's/the/THE/' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
THE tongue is boneless but it breaks bones.12!
google is THE best tools for search keyword. The year ahead will test our political
estavlishment to THE limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
将每行中的第2个1替换为L
[root@localhost ~]# sed 's/1/L/2' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit.
PI=3.14L592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
将文件中的所有 the 替换为 THE
[root@localhost ~]# sed 's/the/THE/g' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
THE tongue is boneless but it breaks bones.12!
google is THE best tools for search keyword. The year ahead will test our political
estavlishment to THE limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
将文件中的所有o删除(替换为空串)
[root@localhost ~]# sed 's/o//g' zz.txt
he was shrt and fat. He was wearing a blue pl shirt with black pants. The hme
f Ftball n BBC Sprt nline.
the tngue is bneless but it breaks bnes.12!
ggle is the best tls fr search keywrd. The year ahead will test ur plitical
estavlishment t the limit.
PI=3.141592653589793238462643383249901429
a wd crss!
Actins speak luder than wrds#wd #
#wd #
AxyzxyzxyzxyC
I bet this place is really spky late at night!
Misfrtunes never cme alne/single.
I shuldn't have lett s tast.
在每行行首插入#号
[root@localhost ~]# sed 's/^/#/' zz.txt
#he was short and fat. He was wearing a blue polo shirt with black pants. The home
#of Football on BBC Sport online.
#the tongue is boneless but it breaks bones.12!
#google is the best tools for search keyword. The year ahead will test our political
#estavlishment to the limit.
#PI=3.141592653589793238462643383249901429
#a wood cross!
#Actions speak louder than words
#
##woood #
##wooooood #
#AxyzxyzxyzxyC
#I bet this place is really spooky late at night!
#Misfortunes never come alone/single.
#I shouldn't have lett so tast.
在包含 the 的每行行首插入#号
[root@localhost ~]# sed '/the/s/^/#/' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
#the tongue is boneless but it breaks bones.12!
#google is the best tools for search keyword. The year ahead will test our political
#estavlishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
在每行行尾插入字符串EOF
[root@localhost ~]# sed 's/$/EOF/' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The homeEOF
of Football on BBC Sport online.EOF
the tongue is boneless but it breaks bones.12!EOF
google is the best tools for search keyword. The year ahead will test our politicalEOF
estavlishment to the limit.EOF
PI=3.141592653589793238462643383249901429EOF
a wood cross!EOF
Actions speak louder than wordsEOF
EOF
#woood #EOF
#wooooood #EOF
AxyzxyzxyzxyCEOF
I bet this place is really spooky late at night!EOF
Misfortunes never come alone/single.EOF
I shouldn't have lett so tast.EOF
将第3~5行中的所有 the 替换为 THE
[root@localhost ~]# sed '3,5s/the/THE/g' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
THE tongue is boneless but it breaks bones.12!
google is THE best tools for search keyword. The year ahead will test our political
estavlishment to THE limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
将包含the的所有行中的o都替换为0
[root@localhost ~]# sed '/the/s/o/0/g' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the t0ngue is b0neless but it breaks b0nes.12!
g00gle is the best t00ls f0r search keyw0rd. The year ahead will test 0ur p0litical
estavlishment t0 the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
迁移符合条件的文本
- H:复制到剪贴
- g,G:将剪贴板中的数据覆盖/追加至指定行
- w:保存为文件
- r:读取指定文件
- a:追加指定内容
将包含 the 的行迁移至文件末尾,{;}用于多个操作
[root@localhost ~]# sed '/the/{H;d};$G' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit.
将第 1~5 行内容转移至第 17 行后
[root@localhost ~]# sed '1,5{H;d};17G' zz.txt
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
将包含 the 的行另存为文件 kk.txt
[root@localhost ~]# sed '/the/w kk.txt' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
将文件/etc/hostname 的内容添加到包含 the 的每行以后
[root@localhost ~]# sed '/the/r /etc/hostname' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!google is the best tools for search keyword. The year ahead will test our politicalestavlishment to the limit.PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
在第 3 行后插入一个新行,内容为 New
[root@localhost ~]# sed '3aNew' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
New
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
在包含 the 的每行后插入一个新行,内容为 New
[root@localhost ~]# sed '/the/aNew' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
New
google is the best tools for search keyword. The year ahead will test our political
New
estavlishment to the limit.
New
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
在第 3 行后插入多行内容,中间的\n 表示换行
[root@localhost ~]# sed '3aNew1\nNew2' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
New1
New2
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
使用脚本编辑文件
使用 sed 脚本将多个编辑指令存放到文件中(每行一条编辑指令),通过“-f”选项来调用
将第 1~5 行内容转移至第 17 行后
[root@localhost ~]# sed '1,5{H;d};17G' zz.txt
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
使用脚本编辑文件
[root@localhost ~]# vi kk.txt
[root@localhost ~]# cat kk.txt
1,5H
1,5d
17G
[root@localhost ~]# sed -f kk.txt zz.txt
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
sed 直接操作文件示例
编写一个脚本,用来调整 vsftpd 服务配置,要求禁止匿名用户,但允许本地用户(也允许写入)
[root@localhost ~]# vim zz.sh
#!/bin/bash
#指定样本文件路径、配置文件路径
SAMPLE="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET SITE/vsftpd .conf"CONFIG="/etc/vsftpd/vsftpd.conf"
#备份原来的配置文件,检测文件名为/etc/vsftpd/vsftpd.conf.bak 备份文件是否存在,若不#存在则使用 cp 命令进行文件备份
[!-e"$CONFIG.bak"]&& cp $CONFIG $CONFIG.bak
#基于样本配置进行调整,覆盖现有文件
sed -e/^anonymous enable/S/YES/NO/g'
$SAMPLE > $CONFIGsed -i -e'/^local enable/s/N0/YES/g'-e '/^write enable/s/NO/YES/g' $CONFIGgrep "listen"$CoNFIGsed -i '$alisten=YES' $CONFIG#启动 vsftpd 服务,并设为开机后自动运行
systemctl restart vsftpd
systemctl enable vsftpd
[root@localhost ~]# bash zz.sh
awk 工具
在 Linux/UNIX 系统中,awk 是一个功能强大的编辑工具,逐行读取输入文本,并根据指定的匹配模式进行查找,对符合条件的内容进行格式化输出或者过滤处理,可以在无交互的情况下实现相当复杂的文本操作,被广泛应用于 shell脚本,完成各种自动化配置任务
通常情况下 awk 所使用的命令格式如下所示,其中,单引号加上大括号“{}”用于设置对数据进行的处理动作。awk 可以直接处理目标文件,也可以通过“-f”读取脚本对目标文件进行处理
awk 选项'模式或条件{编辑指令}'文件1文件2... //过滤并输出文件中符合条件的内容
awk -f 脚本文件 文件1文件2... //从脚本中调用编辑指令,过滤并输出内容
前面提到 sed 命令常用于一整行的处理,而 awk 比较倾向于将一行分成多个“字段”然后再进行处理,且默认情况下字段的分隔符为空格或 tab 键。awk 执行结果可以通过 print 的功能将字段数据打印显示。在使用 awk 命令的过程中,可以使用逻辑操作符“&&”表示“与”、“|”表示“或”、“!”表示“非”;还可以进行简单的数学运算,如+、-、*、/、%、^分别表示加、减、乘、除、取余和乘方
在 Linux 系统中/etc/passwd 是一个非常典型的格式化文件,各字段间使用“:”作为分隔符隔开,Linux 系统中的大部分日志文件也是格式化文件,从这些文件中提取相关信息是运维的日常工作内容之一。若需要査找出/etc/passwd 的用户名、用户 ID、组 ID 等列,执行以下 awk 命令即可
[root@localhost ~]# awk -F ':' '{print $1,$3,$4}' /etc/passwd
root 0 0
bin 1 1
daemon 2 2
adm 3 4
lp 4 7
sync 5 0
shutdown 6 0
halt 7 0
mail 8 12
operator 11 0
games 12 100
ftp 14 50
nobody 65534 65534
systemd-coredump 999 997
dbus 81 81
polkitd 998 996
saslauth 997 76
dhcpd 177 177
sshd 74 74
tss 59 59
unbound 996 993
chrony 995 992
postfix 89 89
awk 从输入文件或者标准输入中读入信息,与 sed 一样,信息的读入也是逐行读取的。不同的是 awk将文本文件中的一行视为一个记录,而将一行中的某一部分(列)作为记录中的一个字段(域)。为了操作这些不同的字段,awk 借用 shel1 中类似于位置变量的方法,用$1、$2、$3…顺序地表示行(记录)中的不同字段。另外 awk 用$0 表示整个行(记录)
不同的字段之间是通过指定的字符分隔。awk默认的分隔符是空格。awk 允许在命令行中用“-F 分隔符”的形式来指定分隔符。在上述示例中,awk 命令对/etc/passwd 文件的处理
- FS:指定每行文本的字段分隔符,默认为空格或制表位。
- NF:当前处理的行的字段个数。
- NR:当前处理的行的行号(序数)
- $ 0:当前处理的行的整行内容
- $n:当前处理行的第n个字段(第n列)
- FILENAME:被处理的文件名
- RS:数据记录分隔,默认为\n,即每行为一条记录
按行输出文本
输出所有内容,等同于 cat zz.txt
[root@localhost ~]# awk '{print}' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
输出所有内容,等同于 cat zz.txt
[root@localhost ~]# awk '{print $0}' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword. The year ahead will test our political
estavlishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words#woood #
#wooooood #
AxyzxyzxyzxyC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
输出第 1~3行内容
[root@localhost ~]# awk 'NR==1,NR==3{print}' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
输出第 1~3 行内容
[root@localhost ~]# awk '(NR>=1)&&(NR<=3){print}' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
输出第 1 行、第 3 行内容
[root@localhost ~]# awk 'NR==1 || NR==3{print}' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
the tongue is boneless but it breaks bones.12!
输出所有奇数行的内容
[root@localhost ~]# awk '(NR%2)==1{print}' zz.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
the tongue is boneless but it breaks bones.12!
estavlishment to the limit.
a wood cross!#wooooood #
I bet this place is really spooky late at night!
I shouldn't have lett so tast.
输出所有偶数行的内容
[root@localhost ~]# awk '(NR%2)==0{print}' zz.txt
of Football on BBC Sport online.
google is the best tools for search keyword. The year ahead will test our political
PI=3.141592653589793238462643383249901429
Actions speak louder than words
#woood #
AxyzxyzxyzxyC
Misfortunes never come alone/single.
输出以 root 开头的行
[root@localhost ~]# awk '/^root/{print}' /etc/passwd
root:x:0:0:Super User:/root:/bin/bash
输出以 nologin 结尾的行
[root@localhost ~]# awk '/nologin$/{print}' /etc/passwd
bin:x:1:1:bin:/bin:/usr/sbin/nologin
daemon:x:2:2:daemon:/sbin:/usr/sbin/nologin
adm:x:3:4:adm:/var/adm:/usr/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/usr/sbin/nologin
operator:x:11:0:operator:/root:/usr/sbin/nologin
games:x:12:100:games:/usr/games:/usr/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/usr/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/usr/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
dbus:x:81:81:D-Bus:/var/run/dbus:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
chrony:x:995:992::/var/lib/chrony:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
统计以/bin/bash 结尾的行数,等同于grep-c"/bin/bash$"/etc/passwd
[root@localhost ~]# awk 'BEGIN {x=o}; /\/bin\/bash$/{x++};END {print x}' /etc/passwd
1
统计以空行分隔的文本段落数
[root@localhost ~]# awk 'BEGIN{RS=""};END{print NR}‘ /etc/squid/squid.conf
按字段输出文本
输出每行中(以空格或制表位分隔)的第3个字段
[root@localhost ~]# awk '{print $3}' zz.txt
short
on
is
the
thecross!
louderthis
come
have
输出每行中的第 1、3 个字段
[root@localhost ~]# awk '{print $1,$3}' zz.txt
he short
of on
the is
google the
estavlishment the
PI=3.141592653589793238462643383249901429
a cross!
Actions louder#woood
#wooooood
AxyzxyzxyzxyC
I this
Misfortunes come
I have
输出密码为空的用户的 shadow 记录
[root@localhost ~]# awk -F ":" '$2=""{print}' /etc/shadow
输出密码为空的用户的 shadow 记录
[root@localhost ~]# awk 'BEGIN {FS=":"}; $2==""{print}' /etc/shado
输出以冒号分隔且第7个字段中包含/bash 的行的第1个字段
[root@localhost ~]# awk -F ":" '$7~"/bash"{print $1}' /etc/passwd
root
zs
输出包含8个字段且第1个字段中包含 nfs 的行的第 1、2 个字段
[root@localhost ~]# awk '($1~"nfs")&&(NF==8){print $1,$2}' /etc/services
nfs 2049/tcp
nfs 2049/udp
nfs 2049/sctp
netconfsoaphttp 832/tcp
netconfsoaphttp 832/udp
netconfsoapbeep 833/tcp
netconfsoapbeep 833/udp
输出第7个字段既不为/bin/bash 也不为/sbin/nologin 的所有行
[root@localhost ~]# awk -F ":" '($7!="/bin/bash")&&($7!="/sbin/nologin"){priunt}' /etc/passwd
通过管道、双引号调用 shell命令
调用 wc -1 命令统计使用 bash 的用户个数,等同于 grep -c"bash$"/etc/passwd
[root@localhost ~]# awk -F: '/bash$/{print | "wc -l"}' /etc/passwd
2
调用w命令,并用来统计在线用户数
[root@localhost ~]# awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}}'
2
调用 hostname,并输出当前的主机名
[root@localhost ~]# awk 'BEGIN { "hostname" | getline ; print $0}'
localhost.localdomain