Shell编程之正则表达式与文本处理器
一、正则表达式
正则表达式(Regular Expression,简称 Regex)是一种强大的文本处理工具,用于在字符串中搜索、匹配或替换特定模式的文本。
1. 基本字符匹配
- 直接匹配普通字符,例如
abc
会匹配字符串中的 “abc”。 - 特殊字符(如
.
*
+
?
()
[]
{}
^
$
\
)需要转义,例如\.
匹配实际的句点。
2. 常用元字符
.
:匹配任意单个字符(除换行符)。\d
:匹配数字(等价于[0-9]
)。\w
:匹配单词字符(字母、数字、下划线)。\s
:匹配空白字符(空格、制表符、换行等)。^
:匹配字符串开头。$
:匹配字符串结尾。
3. 量词(控制重复次数)
*
:0 次或多次。+
:1 次或多次。?
:0 次或 1 次。{n}
:精确匹配 n 次。{n,}
:至少 n 次。{n,m}
:n 到 m 次。
4. 字符类与分组
[abc]
:匹配 a、b 或 c 中的任意一个字符。[a-z]
:匹配小写字母。大写[A-Z][0-9]
: 匹配数字(abc)
:分组,可对子模式进行操作(如捕获或重复)。|
:或操作,例如a|b
匹配 a 或 b。
文本三剑客
1. grep
:文本搜索工具
-
核心功能:在文件中搜索匹配正则表达式的行。
-
正则支持:
-
支持基本正则(BRE)和扩展正则(ERE,需加
-E
参数)。 -
grep
的常用选项 (egrep 扩展正则表达式可以使用更加复杂的正则表达式)语法结构
grep [选项] '模式' [文件...]
常用选项
选项 说明 示例 -i
忽略大小写 grep -i "error" log.txt
-v
反向匹配(排除模式) grep -v "success" data.txt
-w
匹配整个单词 grep -w "word" file.txt
-n
显示匹配行的行号 grep -n "pattern" file.txt
-c
统计匹配行数 grep -c "error" log.txt
示例语法
过滤出不包含the的行
[root@localhost ~]# grep -nv "the" text.txt 1:he was short and fat. 2:he was weating a blue polo shirt with black pants. 3:The home of Football on BBC Sport online. 6:PI=3.14 7:a wood cross! 8:Actions speak louder than words 9: 10:#woood # 11:#woooooooood # 12:AxyzxyzxyzxyzC 13:I bet this place is really spooky late at night! 14:Misfortunes never come alone/single. 15:I shouldn't have lett so tast. 16: ###这里使用了n显示行号v取反 意思就是显示出没有the的行并显示行号
结合正则表达式的语法
利用中括号“[]" 来查找集合字符
[root@localhost ~]# grep -n '[oo]' text.txt 1:he was short and fat. 2:he was weating a blue polo shirt with black pants. 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. 7:a wood cross! 8:Actions speak louder than words 10:#woood # 11:#woooooooood # 13:I bet this place is really spooky late at night! 14:Misfortunes never come alone/single. 15:I shouldn't have lett so tast. ###查找单个字符OO具体'[]'看上面有解释
[root@localhost ~]# grep -n 'sh[oi]' text.txt 1:he was short and fat. 2:he was weating a blue polo shirt with black pants. 15:I shouldn't have lett so tast. ###查找以sh开头后面跟着o或者i两个值其中一个的显示出来
[root@localhost ~]# grep -n '[a-z]' text.txt 1:he was short and fat. 2:he was weating a blue polo shirt with black pants. 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. 7:a wood cross! 8:Actions speak louder than words 10:#woood # 11:#woooooooood # 12:AxyzxyzxyzxyzC 13:I bet this place is really spooky late at night! 14:Misfortunes never come alone/single. 15:I shouldn't have lett so tast. #####匹配小写a到z的所有
[root@localhost ~]# grep -n '^[a-zA-Z]' text.txt 1:he was short and fat. 2:he was weating a blue polo shirt with black pants. 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:PI=3.14 7:a wood cross! 8:Actions speak louder than words 12:AxyzxyzxyzxyzC 13:I bet this place is really spooky late at night! 14:Misfortunes never come alone/single. 15:I shouldn't have lett so tast. ####匹配所有以大小写开头的字母 都显示出来 注意 !!!如果当^在括号内就是取反的意思 不匹配以开头大小写字母的行
使用.匹配任意一位字符或者两个点匹配任意一位字符
[root@localhost ~]# grep "w..d" text.txt google is the best tools for search keyword. a wood cross! Actions speak louder than words ###"."代表匹配任意一个字符 两个点代表匹配任意俩个字符
查询两个字符
[root@localhost ~]# grep -n 'o\{2\}' text.txt 3:The home of Football on BBC Sport online. 5:google is the best tools for search keyword. 7:a wood cross! 10:#woood # 11:#woooooooood # 13:I bet this place is really spooky late at night! ### "o\{2\}" 匹配o两次 这里的“\” 是转义
-
2. awk
:文本分析工具
-
核心功能:按字段处理结构化文本(如 CSV、日志),支持编程逻辑。
-
正则支持:
-
支持正则匹配(
~
操作符)和正则表达式分割字段。语法结构
awk '模式 { 动作 }' 文件名
-
-
模式:决定哪些行会被处理(如正则、条件)。
-
动作:处理匹配行的命令(如打印、计算)。
常用模式
BEGIN
:处理输入前执行(如初始化变量)。END
:处理完所有输入后执行(如汇总结果)。- 正则表达式:
/pattern/
匹配包含模式的行。 - 条件表达式:如
$1 > 100
筛选第一个字段大于100的行。
内置变量
NR
:当前处理的行号。NF
:当前行的字段数。FILENAME
:当前处理的文件名。FS
:输入字段分隔符(默认空格)。OFS
:输出字段分隔符(默认空格)。
示例
显示行号
[root@localhost ~]# awk "{print NR}" test.txt
1
2
3
4
5
6
打印所有行号
[root@localhost ~]# awk '{print $0}' test.txt
he was short and fat.
he was weating a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
3. sed
:流式文本编辑器
-
核心功能:按行处理文本,支持查找、替换、删除等操作。
-
正则支持:
默认使用基本正则,
-E
启用扩展正则。语法结构
sed [选项] '操作' 参数
常用选项
选项 | 功能描述 |
---|---|
-n | 抑制默认输出,需配合 p 命令手动打印行。 |
-e 命令 | 执行多个编辑命令(可叠加)。 |
-f 文件 | 从文件读取编辑命令。 |
-i | 直接修改原文件(危险操作,建议先备份)。 |
-i.bak | 修改原文件并创建备份(如 input.txt.bak )。 |
-r | 使用扩展正则表达式(无需转义特殊字符如 () 、` |
-E | 同 -r (GNU sed 支持)。 |
常用操作
操作 | 功能描述 |
---|---|
a | 增加,在当前行下面增加一行指定内容。 |
c | 替换,将选定行替换为指定的内容。 |
d | 删除,删除选定的行。 |
i | 插入,在选定行上面插入一行指定的内容。 |
p | 打印,如果同时指定行,表示打印指定的行;如果不指定行则表示打印所有内容;如果有非打印字符,则以ASCII码输出。其通常与-n使用 |
s | 替换,替换指定字符。 |
y | 字符转换。 |
示例语法
输出符合条件的文本 (p表示正常输出 “print”)
[root@localhost ~]# sed -n "p" text.txt
he was short and fat.
he was weating 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.
PI=3.14
a wood cross!
Actions speak louder than words
#woood #
#woooooooood #
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 -n "3p" text.txt
The home of Football on BBC Sport online.
输出三到五行内容
[root@localhost ~]# sed -n '3,5p' text.txt
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.
输出所有奇数行
[root@localhost ~]# sed -n "p;n" text.txt
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
a wood cross!
#woooooooood #
I bet this place is really spooky late at night!
I shouldn't have lett so tast.
####n代表下一个(跳过) 如果是偶数行就是 sed -n 'n;p' 文件名
输出 1 ~10 行 之间的偶数行
[root@localhost ~]# sed -n '1,10{n;p}' text.txt
he was weating a blue polo shirt with black pants.
the tongue is boneless but it breaks bones.12!
PI=3.14
Actions speak louder than words
#woood #
#### 如果是奇数行的花啊则需要把 n和p的位置调换一下
输出5行到末尾的奇数
[root@localhost ~]# sed -n "5,$ {p;n}" text.txt
google is the best tools for search keyword.
a wood cross!
#woooooooood #
I bet this place is really spooky late at night!
I shouldn't have lett so tast.
### ^ 代表开头 $ 代表末尾上面做解释了 这里的"5,$ {p;n}" $ 和{} 中间要用空格隔开