`
tomhibolu
  • 浏览: 1383286 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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
交换了前后字段。

分享到:
评论

相关推荐

    unix shell 常用正则表达式

    unix shell 常用正则表达式 很多东西哦

    UNIX命令及SHELL编程

    这是一套完整的Unix培训教材,包括Unix常用命令及SHELL编程基础与高级技巧,PDF格式,共30个文件。另有2个Word文档。包内文件清单如下: 01_Shell-文件安全与权限.PDF 02_Shell-使用find和xargs.PDF 03_Shell-...

    LINUX与UNIX SHELL编程指南

    第七章 正则表达式介绍 第八章 GERP家族 第九章 AWK介绍 第十章 SED用法介绍 第十一章 合并与分割 第十二章 TR用法 第三部分 登录环境 第十三章 登录环境 第十四章 环境和SHELL变量 第十五章 小结 第四部分 基础...

    UNIX和Linux Shell正则表达式语法介绍

    正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。本文介绍了UNIX和Linux Shell正则表达式的语法。

    linux shell 正则表达式(BREs,EREs,PREs)差异比较.docx

    正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。这些是正则表达式的定义。 由于起源于unix系统,...

    绝版经典 LINUX与UNIX SHELL编程指南 PDF 高清版 [16.3M]

    第七章 正则表达式介绍 第八章 GERP家族 第九章 AWK介绍 第十章 SED用法介绍 第十一章 合并与分割 第十二章 TR用法 第三部分 登录环境 第十三章 登录环境 第十四章 环境和SHELL变量 第十五章 小结 第四...

    LINUX与UNIX SHELL编程指南(很全)

    第7章 正则表达式介绍 49 7.1 使用句点匹配单字符 50 7.2 在行首以^匹配字符串或字符序列 50 7.3 在行尾以$匹配字符串或字符 51 7.4 使用*匹配字符串中的单字符或其重复 序列 51 7.5 使用\屏蔽一个特殊字符的含义 52...

    UNIX和LINUX系统的SHELL正则表达式语法

    UNIX和LINUX系统的SHELL正则表达式语法,很全,很实用,欢迎大家查阅

    正则表达式使用详解及使用实例

    我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或sed shell程序等。此外,象JavaScript这种客户端的脚本语言也提供了对正则表达式的支持。由此可见,...

    linux 正则表达式

    linux unix shell 正则表达式 linux unix shell 正则表达式

    shell编程指南,shell脚本,本书共分五部分全。grep家族,sed,AWK,正则表达式,tr用法

    Linux与unix shell编程指南.rar 第一部分 shell 第1章 文件安全与权限 1 第2章 使用find和xargs 14 第3章 后台执行命令 22 第4章 文件名置换 31 第5章 shell输入与输出 34 第6章 命令执行顺序 46 第二部分 文本过滤...

    Linux与unix shell编程指南

    本书共分五部分,详细介绍了shell编程技巧,各种UNIX命令及语法,还涉及了UNIX下的文字处理以及少量的系统管理问题。本书内容全面、文字简洁流畅,适合Shell编程人员学习、参考。 目 录 译者序 前言 第一部分 shell ...

    正则全集\正则表达式使用详

    我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或sed shell程序等。此外,象JavaScript这种客户端的脚本语言也提供了对正则表达式的支持。由此可见,...

    Linux与UNIX Shell编程指南.rar

    本书共分五部分 ,详细介绍了SHELL编程技巧,各种UNIX命令及语法,还涉及了UNIX的文字处理以及少量的系统管理问题。本书内容全面,文字简洁流畅,适合SHELL编程人员学习、参考。 目录 第一部分 SHELL 第一章 文件...

    Linux与Unix Shell编程指南(PDF格式,共30章)

    本书共分五部分,详细介绍了shell编程技巧,各种UNIX命令及语法,还涉及了UNIX下的文字处理以及少量的系统管理问题。本书内容全面、文字简洁流畅,适合Shell编程人员学习、参考。 目 录 译者序 前言 第一部分 ...

    linux基础与shell编程

    linux基础命令讲解 shell编程讲解 LINUX与UNIX Shell编程指南 脚本例子 正则表达式

    Linux与UNIX Shell编程指南.

    本书共分五部分 ,详细介绍了SHELL编程技巧,各种UNIX命令及语法,还涉及了UNIX的文字处理以及少量的系统管理问题。本书内容全面,文字简洁流畅,适合SHELL编程人员学习、参考。 目录 第一部分 SHELL 第一章 文件...

Global site tag (gtag.js) - Google Analytics