目次
正则表达式的界说
正则表达式的用途
基础正则表达式
基础正则表达式示例
查找特定字符
利用中括号“【】”来查找集合字符
查找行首“^”与行尾字符“$”
查找恣意一个字符"."与重复字符"*"
查找连续字符范围"{}"
查询两个0的字符
查询以 w 开头以d结尾,中心包含2~5个0的字符串
查询以 开头以d结尾,中心包含2个或2个以上0的字符串
元字符总结
扩展正则表达式
文本处理器
sed工具
输出符合条件的文本
删除符合条件的文本
替换符合条件的文本
迁徙符合条件的文本
使用脚本编辑文件
sed直接操作文件示例
awk工具
示例按行输出文本
按字段输出文本
通过管道、双引号调用Shell下令
正则表达式的界说
正则表达式,又称规则表达式,(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中具有特殊意义义,以是在使用"{}"字符时,需要利用转义字符"\",将"{}"字符转换成普通字符
查询两个0的字符
- [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!
复制代码
查询以 w 开头以d结尾,中心包含2~5个0的字符串
- [root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
- 8:a wood cross!
- 11:#woood
复制代码
查询以 开头以d结尾,中心包含2个或2个以上0的字符串
- [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工具
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.
复制代码
sed直接操作文件示例
[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
awk工具
在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,即每行为一条记录
示例按行输出文本
按字段输出文本
通过管道、双引号调用Shell下令
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |