目录
正则表达式,又称规则表达式,(Regular Expression,在代码中常简写为regex、regexp或RE),它是一种文本模式,同时也是计算机科学的一个概念,其中包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符")。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串,通常被用来检索、替换那些符合某个模式(规则)的文本许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开来的,后来才逐渐被广泛运用于Scala 、PHP、C# 、Java、C++ 、Objective-c、Perl 、Swift、VBScript 、Javascript、Ruby 以及Python等等。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen
正则表达式(Regular Expression),通常简称为 regex,是一种强大的文本处理工具,它使用预定义的字符序列模式来搜索、匹配和操作文本。正则表达式可以用于各种文本处理任务,如数据验证、数据抽取、字符串解析、复杂的文本替换等正则表达式由普通字符(如字母和数字)和特殊字符(称为"元字符")组成。普通字符包括所有大写和小写字母、所有数字、所有标点符号和一些其他符号。而元字符具有特殊的含义,用于构建强大的匹配模式。以下是一些常用的元字符和它们的功能
正则表达式的字符串表达方法根据不同的严谨程度与功能分为为基本正则表达式与扩展正则表达式。基础正则表达式是常用正则表达式最基础的部分。在Linux系统中常见的文件处理工具中grep与sed支持基础正则表达式,而egrep与awk支持扩展正则表达式。掌握基础正则表达式的使用方法,首先必须了解基本正则表达式所包含元字符的含义
[root@localhost ~]# cat test.txt
he was short and fat. He was wearing a blue poolo shirt with black pants. The home
of Football on BBC Sport online.
the tongue is boneless but it breaks bones.122!
google is the best tools for search keyword. The year ahead will test our political
establishment to the limit. PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#Woood #
#Woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
查找特定字符非常简单,如执行以下命令即可以从文件中查找出特定字符"the"所在位置。其中"-n"表示显示行号、"-i"表示不区分大小写。命令执行后,符合匹配标准的字符,字颜色会变为红色
[root@localhost ~]# grep -n 'the' test.txt
4:the tongue is boneless but it breaks bones.12!!
5:google is the best tools for search keyword. 6:The year ahead will test our political
establishment to the limit.
[root@localhost ~]# grep -in 'test.txt
3:The home of Football on BBC Sport online. 4:the tongue is boneless but it breaks
bones. 12!
5:google is the best tools for search keyword. 6:The year ahead will test our political
establishment to the limit.
若反向选择,如查找不包含"the"字符的行,则需要通过grep命令的"-v"选项实现,并配合"-n"
一起使用显示行号
[root@localhost ~]# grep -vn 'the' test.txt
1:he was short and fat. 2:He was wearing a blue poloshirt with black pants. 3:The
home of Football on BBC Sport online. 7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:#woood #
12:#woooooood #
13:AxyzxyzxyzxyzC
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single. 16:I shouldn't have lett so tast.
想要查找"shirt"与"short"这两个字符串时,可以发现这两个字符串均包含"sh"与"rt"。此时执行以下命令即可同时查找到"shirt"与"short"这两个字符串,其中1"[]"中无论有几个字符,都仅代表一个字符,也就是说"[io]"表示匹配"i"或者"o"
[root@localhost ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat. 2:He was wearing a blhue polo shirt with black pants.
若想重复查找oo可执行
[root@localhost ~]# grep -n 'oo' test.txt
3:The home of Football on BBC Sport online. 5:google is the best tools for search
keyword. 8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
若查找"oo"前面不是"w"的字符串,只需要通过集合字符的反向选择"[^]"来实现该目的。例如执行
"grep-n'[^w]oo'test.txt"命令表示在 test.txt 文本中查找"oo"前面不是"w"的字符串
[root@localhost ~]# grep -n '[^w]oo' test.txt
3:The home of Football on BBC Sport online. 5:gooogle is the best tools for search
keyword. 11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
若不希望"oo"前面存在小写字母,可以使用"grep-n'[a-z]oo'test.txt"命令实现,其中a-z"
表示小写字母,大写字母则通过"A-Z"表示
[root@localhost ~]# grep -n '[^a-z]oo' test.txt
3:The home of Football on BBC Sport online.
查找包含数字的行可以通过"grep-n'[0-9]'test.txt"命令来实现
[root@localhost ~]# grep -n '[0-9]' test.txt
4:the tongue is boneless but it breaks bones.12
7:PI=3.141592653589793238462643383249901429
查找任意一个字符"."与重复字符"*"
前面提到,在正则表达式中小数点(.)也是一个元字符,代表任意一个字符。例如执行以下命令就可以查找"w??d"的字符串,即共有四个字符,以w开头d结尾
[root@localhost ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyworrd.
8:a wood cross!
9:Actions speak louder than words
[root@localhost ~]# grep -n 'ooo*' test.txt
3:The home of Football on BBC Sport online. 5:ggoogle is the best tools for search
keyword. 8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
查询以w开头d结尾,中间包含至少一个o的字符串,执行以下命令即可实现
[root@localhost ~]# grep -n 'woo*d' test.txt
8:a wood cross!
11:#woood
12:#woooooood #
执行以下命令即可查询以w开头d结尾,中间的字符可有可无的字符串
[root@localhost ~]# grep -n 'w.*d' test.txt
1:he was short and fat. 5:google is the best toools for search keyword. 8:a wood cross!
9:Actions speak louder than words
11:#woood #
12:#woooooood #
执行以下命令即可查询任意数字所在行
[root@localhost ~]# grep -n '[0-9][0-9]*' test.t>xt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
查找行首"^"与行尾字符"$"
基础正则表达式包含两个定位元字符:"^"(行首)与"$"(行尾)。在上面的示例中,查询"the"字符串时出现了很多包含"the"的行,如果想要查询以"the"字符串为行首的行,则可以通过"^"元字符来实现
[root@localhost ~]# grep -n ' test.txt
4:the tongue is boneless but it breaks bones.12!
查询以小写字母开头的行可以通过"^[a-z]"规则来过滤,查询大写字母开头的行则使用"^[A-Z]"规
则,若查询不以字母开头的行则使用"^[^a-zA-Z]"规则
[root@localhost ~]# grep -n '^[a-z]' test.txt
1:he was short and fat. 4:the tongue is boneless bbut it breaks bones.12!
5:google is the best tools for search keyword. 8:a wood cross!
[root@localhost ~]# grep -n '^[A-Z]' test.txt
2:He was wearing a blue polo shirt with black pants. 3:The home of Football on BBC
Sport online. 6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
9:Actions speak louder than words
13:AxyzxyzxyzxyzC
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single. 16:I shhouldn't have lett so tast
[root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
11:#woood #
12:#woooooood #
"^"符号在元字符集合"[]"符号内外的作用是不一样的,在"[]"哥哥内表示反向选择,在"[]"符号
外则代表定位行首。反之,若想查找以某一特定字符结局尾的行则可以使用"$"定位符。例如,执行以下命令即可实现查询以小数点(.)结尾的行。因为小数点(.)在正E则表达式中也是一个元字符所以这里需要用定义字符“\”将具有特殊意义的字符转换为普通字符
[root@localhost ~]# grep -n '\'\' test.txt
1:he was short and fat. 2:He was wearing a blue polo :shirt with black pants. 3:The
home of Football on BBC Sport online. 5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit. 15:Misfortunes
never come alone/single. 16:I shouldn't have llett so tast
当查询空白时, 执行"grep-n'^$'test.txt"命令即可
[root@localhost ~]# grep -n'^$
test.txt
10:
在上面的示例中,使用了"."与"*"来设定零个到无限多个重多个重复的字符,如果想要限制一个范围内的重复的字符串该如何实现呢?例如,查找三到五个0的连续字符,这个时候就需要使用基础正则表达式中的限定范围的字符"{}"。因为"{}"在Shell中具有特殊意义义,所以在使用"{}"字符时,需要利用转义字符"\",将"{}"字符转换成普通字符
[root@localhost ~]# grep -n 'o\{2\}' test.txt
3:The home of Football on BBC Sport online. 5:googlhe is the best tools for search
keyword. 8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late atnight!
[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross!
11:#woood
[root@localhost ~]# grep -n 'wo\{2,\'}d' test.txtt
8:a wood cross!
11:#woood #
12:#woooooood #
字符 | 说明 |
\ | 将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符 |
^ | 匹配输入字符串的开始位置 |
* | 匹配前面的子表达式零次或多次 |
+ | 匹配前面的子表达式一次或多次 |
? | 匹配前面的子表达式零次或一次 |
. | 匹配除换行符(\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 | 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[\f\n\\t\v] |
\S | 匹配任何非空白字符。等价于[^\f\n\r\t\v] |
\w | 匹配字母、数字、下划线。等价于'A-Za-z0-9」 |
\W | 匹配非字母、数字、下划线。等价于'[^A-Za-z0-9]' |
\n | 匹配一个换行符 |
\f | 匹配一个换页符 |
\r | 匹配一个回车符 |
$ | 匹配输入字符串的结束位置 |
元字符 | 作用与示例 |
+ | 作用:重复一个或者一个以上的前一个字符 示例:执行"egrep-n'wo+d'test.txt"命令,即可查询"wood""wood""woodod""wooooooood"等字符串 |
? | 作用:零个或者一个的前一个字符 示例:执行"egrep-n'bes?t'test.txt"命令,即可查询"bet""best"这两个字符串 |
| | 作用:使用或者(or)的方式找出多个字符 示例:执行"egrep-n'ofjislon'test.txt"命令即可查询"of"或者"on"字符串 |
() | 作用:查找"组"字符串 示例:"egrep-n't(ale)st'test.txt"。"tast"与"test"因为这两个单词的"t"与"st"是重复的,所以将"a"与"e" 列于"0"符号当中,并以"分隔,即可查询"tast"或者"test"字符串 |
()+ | 作用:辨别多个重复的组 示例:"egrep-n'A(xyz)+C'test.txt"。该命令是查询开头的"A"结尾是"C",中间有一个以上的"xyz"字 符串的意思 |
在Linux/UNIX系统中包含很多种类的文本处理器或文本编辑器,其中包括我们之前提过的VIM编辑器与grep等。而grep,sed,awk更是Shell编程中经常用到的文本处理工具,被称之为Shell编程三剑客
sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。sed也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于Shell脚本中,用以完成各种自动化处理任务sed的工作流程主要包括读取,执行和显示三个过程
读取:sed从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,patternspace)
执行:默认情况下,所有的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:字符转换
[root@localhost ~]# sed -n 'p' test.txt
he was short and fat. He was wearing a blue polo shirt with black pants. The home
of Football
on BBC Sport online.
[root@localhost ~]# sed -n\'3ptest.txt
The home of Football on BBC Sport online.
[root@localhost ~]#
sed -n '3,5p
test.txt
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.112!
google is the best tools for search keyword.
[root@localhost ~]# sed -n 'p;n' test.txt
he was short and fat. The home of Football on BBC Spport online. google is the best
tools for search keyword
[root@localhost ~]# sed -n 'n;p' test.txt
He was wearing a blue polo shirt with black pantsS.
the tongue is boneless but it breaks bones.12!
The year ahead will test our political establishment to the limit
[root@localhost ~]# sed -n '1,5{p;n}' test.txt
he was short and fat. The home of Football onBBC Sport online. google is the best
tools for search keyword.
[root@localhost ~]# sed -n '10,${n;p}' test.txt
#Woood #
AxyzxyzxyzxyzC
Misfortunes never come alone/single.
sed命令与正则表达式结合使用的示例
[root@localhost ~]# sed -n '/the/p' test.txt
the tongue is boneless but it breaks bones.112!
google is the best tools for search keyword. The yeaar ahead will test our political
establishment to the limit.
[root@localhost ~]# sed -n '/the/=' test.txt
4
5
6
[root@localhost ~]# sed -n '/^PI/p' test.txt
PI=3.141592653589793238462643383249901429
[root@localhost ~]# sed -n '/[0-9]$/p'test.txt
PI=3.141592653589793238462643383249901429
[root@localhost ~]# sed -n '\<wood\>/p' test.txt
a wood cross!
下面命令中nl命令用于计算文件的行数,结合该命令可以更加直观地查看到命令执行的结果
[root@localhost ~]# nl test.txt | sed '3d'
1 he was short and fat. 2 He was wearing a blue polo shirt with black pants. 4 the
tongue is boneless but it breaks bones.12!
5 google is the best tools for search keyword. 6 The yyear ahead will test our political
establishment to the limit. .....
[root@localhost ~]# nl test.txt | sed '3,5d'
1 he was short and fat. 2 He was wearing a bluue polo shirt with black pants. 6 The
will
to
the
limit. 7
ahead
test
our
political
establishment
year
PI=3.1415926535897932384626433832499014299
8 a wood cross!
[root@localhost ~]# nl test.txt |sed '/cross/d'
7 P522,141582653589781231415832199914
9 Actions speak louder than words
[root@localhost ~]# sed '/^[a-z]/d' test.txt
He was wearing a blue polo shirt with black pants. The hoome of Football on BBC Sport
online. The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
Actions speak louder than words
#woood #
#Woooooood #
Axyzxyzxyzxyzc
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' test.txt
the tongue is boneless but it breaks bones.i12!
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
[root@localhost ~]# sed '/^$/d' test.txt
he was short and fat. He was wearing a blue poloshirt 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. Theyear ahead will test our political
establishment to the limit. PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
在使用sed命令进行替换操作时需要用到s(字符串替换)、c(整行/整块替换)、y(字符转换)命令选项,常见的用法如下所示
在使用sed命令迁移符合条件的文本时,常用到以下参数:
H:复制到剪贴板;
g、G:将剪贴板中的数据覆盖/追加至指定行;
W:保存为文件;
r:读取指定文件;
a:追加指定内容
示例
[root@localhost ~]# vi opt.list
1,5H
1,5d
17G
[root@localhost ~]# sed -f opt.list test.txt
The year
ahead
will
political
establishment
to
the
limit.
test
our
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast. he was short and fat.He was wearing a blue polo shirt
with black pants. The home of Football on BBC Spoort online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
[root@localhost ~]# vim local_only_ftp.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> $CONFIG
sed -i -e '/^local_enable/s/NO/YES/g' -e '/^write_enable/s/NO/YES/g' $CONFIG
grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG
#启动vsftpd服务,并设为开机后自动运行
systemctl restart vsftpd
systemctl enable vsftpd
[root@localhost ~]# chmod +x local_only_ftp.sh
在Linux/UNIX系统中,awk是一个功能强大的编辑工具,逐行读取输入文本,并根据指定的匹配模式进行查找,对符合条件的内容进行格式化输出或者过滤处理,可以在无交互的情况下实现相当复杂的文本操作,被广泛应用于Shell脚本,完成各种自动化配置置任务awk 是一种强大的文本处理工具,广泛应用于 Linux 和 Unix 系统中。它通过提供编程语言的功能,如变量、数学运算、字符串处理等,使得对文本文件的分析和操作变得非常灵活和高效
基本语法
awk [options] 'pattern {action}' file
options:控制 awk 的行为。
pattern:匹配输入数据的模式。如果省略,则 awk 将对所有行进行操作。
action:在匹配到模式的行上执行的动作。如果省略,则默认动作是打印整行
-F <分隔符>:指定输入字段的分隔符,默认是空格。
-v <变量名>=<值>:设置 awk 内部的变量值。
-f <脚本文件>:指定一个包含 awk 脚本的文件。
-V 或 --version:显示 awk 的版本信息。
-h 或 --help:显示 awk 的帮助信息
[root@localhost ~]# awk -F ':' '{print $1,$3,$4}' /etc/passwd
root 0 0
bin 1 1
daemon 2 2
awk包含几个特殊的内建变量(可直接用)如下所示:
FS:指定每行文本的字段分隔符,默认为空格或制表位
NF:当前处理的行的字段个数
当前处理的行的行号(序数)
INR:
$0:当前处理的行的整行内容
$n:
当前处理行的第 n个字段(第 n 列)
FILENAME:被处理的文件名
RS:数据记录分隔,默认为\n,即每行为一条记录