UNIX Shell 编程(3)-UNIX Shell的正则表达式

简介: 版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/4073384 UNIX Shell 编程(3)-UNIX Shell的正则表达式 匹配任何单个字符:句点(.)比如:r.表示匹配r后跟任一个字符的模式。
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/4073384

UNIX Shell 编程(3)-UNIX Shell的正则表达式

 



匹配任何单个字符:句点(.)
比如:r.表示匹配r后跟任一个字符的模式。

匹配行首符号:^
比如:^George表示匹配以George开头的行

匹配行尾符号:$
比如:contents$表示匹配在行尾的字符串contents。

GNU Ed 0.8的用法
[root@localhost programs]# ed intro 
253
/.../ #搜索前后都有空格的任意三个字符
The Unix operating system was pioneered by Ken 
/ #重复上次搜索
Thompson and Dennis Ritchie at Bell Laboratories 
/
in the late 1960s. One of the primary goals in 
/
the design of the Unix system was to create an 
/
environment that promoted efficient program 
/
developments.
1,$p
The Unix operating system was pioneered by Ken 
Thompson and Dennis Ritchie at Bell Laboratories 
in the late 1960s. One of the primary goals in 
the design of the Unix system was to create an 
environment that promoted efficient program 
developments.
1,$s/p.o/XXX/g #把所有“p.o”替换为“XXX”
1,$p #查看替换后的结果
The Unix operating system was XXXneered by Ken 
ThomXXXn and Dennis Ritchie at Bell Laboratories 
in the late 1960s. One of the primary goals in 
the design of the Unix system was to create an 
environment that XXXmoted efficient XXXgram 
developments.
/^the/ #搜索以the开头的行
the design of the Unix system was to create an 
1,$s/^/>>/ #在每行开头插入>>
1,$
>>developments.
1,$p #查看插入后结果
>>The Unix operating system was pioneered by Ken 
>>Thompson and Dennis Ritchie at Bell Laboratories 
>>in the late 1960s. One of the primary goals in 
>>the design of the Unix system was to create an 
>>environment that promoted efficient program 
>>developments.
1,$s/^/ / #在每行开头插入空格
1,$p #查看结果
  >>The Unix operating system was pioneered by Ken 
  >>Thompson and Dennis Ritchie at Bell Laboratories 
  >>in the late 1960s. One of the primary goals in 
  >>the design of the Unix system was to create an 
  >>environment that promoted efficient program 
  >>developments.
//.$/ #搜索以句点结束的行
developments.
1,$s/$/>>/ #在每行末尾添加>>
1,$p #查看结果
The Unix operating system was pioneered by Ken >>
Thompson and Dennis Ritchie at Bell Laboratories >>
in the late 1960s. One of the primary goals in >>
the design of the Unix system was to create an >>
environment that promoted efficient program >>
developments.>>
1,$s/..$// #删除每行最好两个字符
1,$p #查看结果
The Unix operating system was pioneered by Ken 
Thompson and Dennis Ritchie at Bell Laboratories 
in the late 1960s. One of the primary goals in 
the design of the Unix system was to create an 
environment that promoted efficient program 
developments.

匹配字符组之一:[...]结构
/the/ 匹配含the的行
[tT]he 匹配the或The
如:
1,$s/[aeiouAEIOU]//g #删除所有元音字母
1,$p
Th nx prtng systm ws pnrd by Kn 
Thmpsn nd Dnns Rtch t Bll Lbrtrs 
n th lt 1960s. n f th prmry gls n 
th dsgn f th nx systm ws t crt n 
nvrnmnt tht prmtd ffcnt prgrm 
dvlpmnts.

[0-9] 匹配数字
[a-z] 匹配小写字母
[a-zA-Z] 匹配大小写字母
如:
1,$s/[A-Z]/*/g #把所有大些字母替换为*
1,$p
*he *nix operating system was pioneered by *en 
*hompson and *ennis *itchie at *ell *aboratories 
in the late 1960s. *ne of the primary goals in 
the design of the *nix system was to create an 
environment that promoted efficient program 
developments.
[^A-Z] 匹配除大些字母以外的任何字符

匹配零个到若干个字符:星号(*)
如下,把多个空格替换成一个空格:
[root@localhost programs]# ed lotsapaces 
126
1,$p
This is an example of a
file that contains a lot
of blank spaces
1,$s/ */ /g
1,$p
This is an example of a
file that contains a lot
of blank spaces

再如:匹配一行中第一个e和最后一个e之间的所有内容,替换为+++:
[root@localhost programs]# ed lotsapaces 
126
1,$s/e.*e/+++/g
1,$p
This is an +++ of a
file that contains a lot
of blank spaces

1,$s/[a-zA-Z][a-zA-Z]*/X/g
1,$p
X X X +++ X X
X X X X X
X X X

匹配精确数目的字符串:
XX* 指匹配至少一个连续的X
XXX* 指匹配至少两个连续的X
/{min,max/} 其中:min表示重复的最小次数,max表示最大次数。

保存匹配的字符串/(.../):
把字符包括在前加反斜杠的小括号中,可以捕获正则表达式匹配的字符串。
捕获的字符串存储在编号为1到9的寄存器中。
如下:
[root@localhost programs]# ed phonebook 
155
1,$p
Alica Chebba 973-555-2015
Barbara Swingle 201-555-9257
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony Iannino 973-555-1295
1,$s//(.*/) /(.*/)//2 /1/
1,$p
973-555-2015 Alica Chebba  
201-555-9257 Barbara Swingle  
212-555-2298 Liz Stachiw  
201-555-7776 Susan Goldberg  
973-555-1295 Tony Iannino  
交换了前后字段。

目录
相关文章
|
3月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
3月前
|
Shell Linux
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
|
1月前
|
Shell Linux Windows
让我们熟悉一下 shell 正则表达式使用
正则表达式是一种文本处理工具,用于匹配、查找、替换或提取字符串中的特定模式。通过普通字符和特殊字符(元字符)组成,定义匹配规则。本文档通过示例展示了如何使用正则表达式进行字符串搜索、过滤和模式匹配,包括基本匹配、行首行尾定位、字符集使用、任意字符与重复字符处理以及限定重复次数等高级功能。
46 7
|
1月前
|
运维 监控 Shell
深入理解Linux系统下的Shell脚本编程
【10月更文挑战第24天】本文将深入浅出地介绍Linux系统中Shell脚本的基础知识和实用技巧,帮助读者从零开始学习编写Shell脚本。通过本文的学习,你将能够掌握Shell脚本的基本语法、变量使用、流程控制以及函数定义等核心概念,并学会如何将这些知识应用于实际问题解决中。文章还将展示几个实用的Shell脚本例子,以加深对知识点的理解和应用。无论你是运维人员还是软件开发者,这篇文章都将为你提供强大的Linux自动化工具。
|
2月前
|
Shell
Shell编程(下)
Shell编程(下)
111 1
|
2月前
|
Shell Linux Windows
Shell编程(上)
Shell编程(上)
49 1
|
2月前
|
Shell Linux 开发工具
|
2月前
|
监控 Unix Shell
shell脚本编程学习
【10月更文挑战第1天】shell脚本编程
84 12
|
2月前
|
算法 Unix 数据安全/隐私保护
Python编程--UNIX口令破解机
Python编程--UNIX口令破解机
29 1
|
3月前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余