Shell 编程 正则表达式

本篇主要写一些shell脚本正则表达式的使用基础。


概述

正则表达式分为基础正则表达式(Regular Expression)与扩展正则表达式(Extended Regular Expression)。

它不是一个工具程序,而是一个字符串处理的标准依据,是使用单个字符串搜索、匹配一系列符合某个语法规则的字符串。

它由普通字符(a~z)以及特殊字符(元字符)组成。

linux 文本处理工具

文本处理工具 基础正则表达式 扩展正则表达式
vi编辑器 支持 /
grep 支持 /
egrep 支持 支持
sed 支持 /
awk 支持 支持

基础正则表达式

  • 基础正则表达式常见元字符

^:匹配输入字符串的开始位置。在方括号表达式中使用,表示不包含该字符集合。要匹配^字符本身,使用\^
$:匹配输入字符串的结尾位置。如果设置了RegExp对象的Multiline属性,则$也匹配\n\r。要匹配$字符本身,请使用\$
.:匹配除\r\n之外的任何单个字符
\:将下一个字符标记为特殊字符、原义字符、向后引用、八进制转义符。例如,n匹配字符n\n匹配换行符。序列\\匹配\,而\(则匹配(
*:匹配前面的子表达式零次或多次。要匹配*字符,请使用\*
[]:字符集合。匹配所包含的任意一个字符。例如,[abc]可以匹配plain中的a
[^]:赋值字符集合。匹配未包含的一个任意字符。例如,[^abc]可以匹配plainplin中的任何一个字母
[n1-n2]:字符范围。匹配指定范围内的任意一个字符。例如,[a-z]可以匹配az范围内的任意一个小写字母字符。
{n}n是一个非负整数,匹配确定的n次。例如,o{2}不能匹配Bob中的o,但是能匹配food中的两个o
{n,}n是一个非负整数,至少匹配n次。例如,o{2,}不能匹配Bob中的o,但能匹配foooood中的所有oo{1,}等价于o+o{0,}则等价于o*
{n,m}mn均为非负整数,其中n<=m,最少匹配n次且最多匹配m

  • 使用grep做示例,首先准备一个测试文件
he was short and fat.
He was wearing 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.
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.

特定字符

-n:显示行号
-i:不区分大小写
-v:反向选择

[[email protected] ~]# 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.
[[email protected] ~]# grep -in 'the' 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.
[[email protected] ~]# grep -vn 'the' 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.
7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:
12:#woood #
13:#woooooood #
14:AxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.

集合“[]”

  • 中括号“[]”中无论又几个字符,都只匹配其中一个
[[email protected] ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
  • 匹配重复单个字符oo
[[email protected] ~]# 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!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
  • 查找oo前不是w的字符串
[[email protected] ~]# grep -n '[^w]oo' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
  • 查找oo前没有小写字母的字符串
[[email protected] ~]# grep -n '[^a-z]oo' test.txt
3:The home of Football on BBC Sport online.
  • 查找包含数字的行
[[email protected] ~]# grep -n '[0-9]' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429

行首“^”

  • 查找已the为行首的行
[[email protected] ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
  • 查找是小写字母开头的行
[[email protected] ~]# grep -n '^[a-z]' test.txt
1:he was short and fat.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
8:a wood cross!
  • 查找是大写字母开头的行
[[email protected] ~]# 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
14:AxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
  • 查找不是字母开头的行
[[email protected] ~]# grep -n '^[^a-zA-Z]' test.txt
12:#woood #
13:#woooooood #

行尾“$”

  • 查找以.结尾的行,需要使用转义字符
[[email protected] ~]# 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.
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
  • 查找空白行
[[email protected] ~]# grep -n '^$' test.txt
10:
11:

任意一个字符“.”

  • 查找wd中间有两个字符的字符串
[[email protected] ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words

重复字符“*”

  • 查找至少两个以上的o的字符串,*代表重复前面的一个字符零个或多个
[[email protected] ~]# grep -n 'ooo*' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
  • 查找wd中间有两个至少有一个o的字符串
[[email protected] ~]# grep -n 'woo*d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
  • 查找wd中间可有可无的字符串
[[email protected] ~]# grep -n 'w.*d' test.txt
1:he was short and fat.
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
12:#woood #
13:#woooooood #
  • 查找任意数字
[[email protected] ~]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429

连续字符范围“{}”

  • 查找有连续两个o的字符串,需要转义
[[email protected] ~]# grep -n 'o\{2\}' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
  • 查找wd中间包含2~5o的字符串
[[email protected] ~]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross!
12:#woood #
  • 查找wd中间包含两个以上o的字符串
[[email protected] ~]# grep -n 'wo\{2,\}d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #

扩展正则表达式

  • 扩展正则表达式常见元字符

+:重复一个或者一个以上的前一个字符
?:零个或者一个的前一个字符
|:使用或者or的方式找出多个字符
():查找字符串
()+:辨别多个重复的组

  • 使用rgrep做示例,查询wd之间包含一个以上o的字符串
[[email protected] ~]# egrep -n 'wo+d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
  • 查询betbest这两个字符串
[[email protected] ~]# egrep -n 'bes?t' test.txt
5:google is the best tools for search keyword.
15:I bet this place is really spooky late at night!
  • 查询of或者if或者on字符串
[[email protected] ~]# egrep -n 'of|is|on' 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.
9:Actions speak louder than words
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
  • 查询tast或者test字符串
[[email protected] ~]# egrep -n 't(a|e)st' test.txt
6:The year ahead will test our political establishment to the limit.
17:I shouldn't have lett so tast.
  • 查询开头的A结尾是C,中间有一个以上的xyz字符串
[[email protected] ~]# egrep -n 'A(xyz)+C' test.txt
14:AxyzxyzxyzxyzC

原文地址:https://www.cnblogs.com/llife/p/11675283.html

时间: 2024-07-31 02:32:10

Shell 编程 正则表达式的相关文章

shell编程-正则表达式

1.正则表达式是什么 它主要用于字符串的模式分割,匹配,查找及替换操作. 2.正则表达式与通配符 正则表达式用来在文件中匹配符合条件的字符串,正则包含匹配.grep,awk,sed等命令可以支持正则表达式. 通配符用来匹配符合条件的文件名,通配符是完全匹配.ls ,find,cp这些命令不支持正则表达式,所以只能使用shell自己的通配符进行匹配了. 3.基础正则表达式 .相当于通配符的? [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{1,3\}\.[0-9]\

Linux Shell编程 - 正则表达式

一.基础正则表达式 1.正则表达式:正则表达式是用来在文件中匹配符号条件的字符串,正则是包含匹配 grep.awk.sed等命令可以支持正则表达式. 2.通配符:用来匹配符合条件的文件名,通配符是完全匹配.ls.find.cp这些命令不之处正则表达式, 所以只能使用 shell 自己的通配符来进行匹配了. 3.基础正则表达式

Linux学习 -- Shell编程 -- 正则表达式

正则表达式与通配符 正则 -- 匹配字符串 -- 包含匹配     grep.awk.sed等 通配符 -- 匹配文件名 -- 完全匹配  ls.find.cp等 基础正则表达式

05 shell编程之正则表达式

正则表达式&&文本处理利器 学习目标: l  掌握正则表达式的运用 l  掌握sed.awk文本处理工具的使用 目录结构:   正则表达式 正则表达式概述 l  正则表达式:使用单个字符串来描述,匹配一系列符合某个句法规则的字符串 l  由普通字符与特殊字符组成 l  一般用在脚本编程,文本编辑器中,如php.Python.shell等,简写为regex.regexp.RE l  用来检索.替换符合模式的文本,具有强大的文本匹配功能 l  能够在文本海洋中快速高效地处理文本 l  正则表达

Shell编程之正则表达式(二)

文本处理器 在 Linux/UNIX 系统中包含很多种文本处理器或文本编辑器,其中包括我们之前学习过的VIM 编辑器与 grep 等.而 grep.sed.awk 更是 shell 编程中经常用到的文本处理工具,被称之为 Shell 编程三剑客. sed 工具 sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除.替换.添加.移动等),最后输出所有行或者仅输出处理的某些行.sed 也可以在无交互的情况下实现相复杂的文本处理

shell编程(七)--- sed的用法

sed:称为流编辑器,也称为行编辑器. sed处理过程:sed读取源文件中的一行,并将其放在临时缓冲区中,这个缓冲区称为模式空间,使用相应的模式对模式空间进行处理,并将处理的结果显示到屏幕上,并不会影响源文件,处理结束后,将模式空间中的内容显示至屏幕. sed格式: sed [options] 'AddressCommand' file ... options: -n:静默模式,不显示模式空间中的内容,默认显示默认空间中的内容.常和p命令一起使用. -i:直接修改源文件的内容. -e 'Addr

Linux Shell编程入门

从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操作.在Linux GUI日益完善的今天,在系统管理等领域,Shell编程仍然起着不可忽视的作用.深入地了解和熟练地掌握Shell编程,是每一个Linux用户的必修 功课之一. Linux的Shell种类众多,常见的有:Bourne Shell(/usr/bin/sh或/bin/sh).Bourne

Shell编程(脚本)的常用命令和语句

一些常用的Shell编程(脚本)命令和语句,可以满足一般需求. 接收到的命令参数: 参数个数: $# 参数值: 命令本身:$0 第一个参数:$1 第二个参数:$2 -- 退出命令: exit echo命令: 换行: echo 输出后不换行: echo -n "请选择(y/n)?" 利用转义符号输出双引号: echo "欢迎使用\"正式服务器\"部署工具." 输出中带变量: echo "即将部署项目:$project_name"

linux中shell编程

shell编程 1 echo -e 识别\转义符 \a \b \t \n \x十六进制 \0八进制 等等 #!/bin/bash echo -e "hello world" 执行脚本:方式1 :chmod 755 hello.sh ./hello.sh 方式2 :bash ./hello.sh(这种方式不需要给执行权限) 1 历史命令 history 直接回车就可以看到已经敲过得命令.-c清空缓存中和文件中的命令 -w将缓存中命令写入 家目录/.bash_history 这个命令可以帮