linux_shell_5_shell特性_正则_1
前面我们了解了部分linux shell的相关特性,下面的链接是第4篇文章:linux_shell_4_shell特性
这里我们来继续讨论linux shell中至关重要的一个特性: 正则表达式 (regular expression)。
正则表达式主要是用来处理字符流的,它的处理单位是行字符文本,也就是说正则表达式的处理对象是: 字符行。
【1】单字符通配
在bash中,我么可以使用 ? 来匹配单个字符,但是在正则表达式中,? 不能用作单个字符的通配。这一点需要引起注意。
在正则表达式中利用:
[a-z] #匹配lowcase char
[A-Z] #匹配upcase char
[a-zA-Z] #匹配任意char 字符
这三个表达式仅表示通配一个字符。
这里我们以鸟哥的一个在线文档为例: http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt
你可以利用wget命令获取这个文档。
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. .
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird
Exp: 查找含有 test 或者 taste 的行
[volcanol@volcanol ~]$ grep -n --color=auto 't[ae]st' regular_express.txt
:I can't finish the test.
:Oh! The soup taste good.
这里我们可以看到第八行含有一个 test 字符序列, 与 t[ae]st 真好可以匹配一个; 而第九行 taste 也正好匹配一个,当然如果文档有如果
包含里 test 或者 tast 字符序列的那么也将会显示出来。如果要同时查找仅匹配: test 和 taste的字符串则需要将这个表达式进行修改:
[volcanol@volcanol ~]$ grep -n --color=auto 't[a-z]st' regular_express.txt | grep 't[ae]st[^a-df-z]'
:I can't finish the test.
:Oh! The soup taste good.
利用下面这个命令才能真正的找到test 或者 taste 。
我们可以测试一下:
[volcanol@volcanol ~]$ echo "i can't finish the testf" | grep -n --color=auto 't[a-z]st' | grep 't[ae]st[^a-df-z]' [volcanol@volcanol ~]$ echo "i can't finish the teste" | grep -n --color=auto 't[a-z]st' | grep 't[ae]st[^a-df-z]'
:i can't finish the teste
[volcanol@volcanol ~]$ echo "i can't finish the tastf" | grep -n --color=auto 't[a-z]st' | grep 't[ae]st[^a-df-z]'
可以发现第二个命令可以实现我们需要的功能。
【2】grep
grep是一个用来进行模式匹配的linux下的工具程序,通过这个工具可以进行数据的筛选。其命令如法如下:
SYNOPSIS
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
可以看到grep 有两种命令格式,一般用的比较多的是第一种。即: grep 选项 模式字符串 待查找的文件
这里还需要说明一点: 模式字符串可以用单引号,也可以用双引号, 在bash中,虽然单引号和双引号在某些时候存在一些区别;在进行
模式匹配的时候推荐使用单引号,以免引起一些副作用。例如在查询: “$SHELL” 时 ,“$SHELL” 和‘$SHELL’ 是不相同的。
我们在 regular_expression文件中的最后增加两行内容:
[volcanol@volcanol ~]$ cat regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. .
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird
dido $SHELL
/bin/bash
[volcanol@volcanol ~]$ grep --color=auto "$SHELL" regular_express.txt
/bin/bash
[volcanol@volcanol ~]$ grep --color=auto '$SHELL' regular_express.txt
dido $SHELL
可以发现两个命令输出的结果不一样,下面我们利用grep 进行一些简单的测试。
Exp1: 获取所有含有 the 的行
[volcanol@volcanol ~]$ grep --color=auto 'the' regular_express.txt
I can't finish the test.
the symbol '*' is represented as start.
You are the best is mean you are the no. .
The world <Happy> is the same with "glad".
google is the best tools for search keyword.
这里我们可以看到命令成功执行,每一个输出行都含有一个连续的字符序列: the
Exp:获取所有不含有 the 的行
[volcanol@volcanol ~]$ grep -nv 'the' regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:apple is my favorite food.
:Football game is not use feet only.
:this dress doesn't fit me.
:However, this dress is about $ dollars.
:GNU is free air not free beer.
:Her hair is very beauty.
:Oh! The soup taste good.
:motorcycle is cheap than car.
:This window is clear.
:Oh! My god!
:The gd software is a library for drafting programs.
:I like dog.
:goooooogle yes!
:go! go! Let's go.
:# I am VBird
:dido $SHELL
:/bin/bash
在这里通过参数-v; 实现反向选择。
Exp: 不区分大小写来选取
grep 通过参数 -i 来实现不去很大小写。
:I can't finish the test.
:Oh! The soup taste good.
:the symbol '*' is represented as start.
:The gd software is a library for drafting programs.
:You are the best is mean you are the no. .
:The world <Happy> is the same with "glad".
:google is the best tools for search keyword.
可以发现第14、16行的The 别选取了。
Exp: 输出结果,显示行号
通过参数-n 实现输出显示行号
[volcanol@volcanol ~]$ grep 'test' regular_express.txt
I can't finish the test.
[volcanol@volcanol ~]$ grep -n 'test' regular_express.txt
:I can't finish the test.
可以发现,加上参数 -n 后,在输出的结果前面多了 8:
Exp:单字符通配
正则表达式的单字符通配与bash shell 的单字符通配存在一些不一样,在bash shell中非正则表达式通配符利用 ? 表示单字符通配; 而
正则表达式中单字符通配,利用 [ ] 来实现。
[volcanol@volcanol ~]$ grep -n 'ab[a-z]' regular_express.txt
:However, this dress is about $ dollars.
如上命令,执行后,红色部分表示在文件中匹配的字符,这里利用 [a-z] 表示匹配 a-z 之间任意一个字符就算匹配成功, 因为模式字符串为
ab[a-z], 因此只要是 aba、abb、abc、.......aby、abz 中有一个匹配上就认为模式匹配成功,因此这里认为about 中的abo 匹配成功,因此输出
about所在的行。
Exp: 行首、行尾匹配限定
有时需要将匹配字符串限定在行首、行尾,这时可以使用行首限定 ^; 或者行尾限定$.
例如,我们仅能匹配行首匹配the的字符串,可以如下实现:
[volcanol@volcanol ~]$ grep -n '^the' regular_express.txt
:the symbol '*' is represented as start.
可以看出只有 the 在行首匹配的这一行输出了。
如果我们想匹配 行尾的 d 字符的一行,可以如下实现:
[volcanol@volcanol ~]$ grep -n 'd$' regular_express.txt
:# I am VBird
可以看出,只有最后一个字符是d的21行被选择输出了。
Exp: 注意方向选择行与排除字符在外,以及^符号作为行首和排除字符的区别。
当将^字符放在模式字符串的开头的时候,表示行首,当将^ 放在 [ ]中的时候表示排除,例如我们可以用下面的命令来表示空白行:
[volcanol@volcanol ~]$ grep -n '^$' regular_express.txt
:
:
可以发现文章的第24、25行是空白行。
这里小技巧我们可以利用cat 命令查看,空白行在linux中是怎么表示的:
cat -An regular_express.txt
"Open Source" is a good mechanism to develop programs.$
........
goooooogle yes!$
go! go! Let's go.$
# I am VBird$
dido $SHELL$
/bin/bash$
$
$
可以看到,在linux中,空白行只有$符号表示空白行,这个与Win有点不一样。
Exp: 任意字符 . 和重复字符 *
在正则表达式里,任意字符与通常的通配符不一样,在正则表达式里面,任意字符用 . 号表示,而用 * 号表示重复。例如:
[volcanol@volcanol ~]$ grep -n 'a.c' regular_express.txt
:google is the best tools for search keyword.
如上所示,arc 就匹配里这个模式字符串 a.c ; 修改一下文件,然后在进行测试。
:google is the best tools for search keyword.
:///abc
可以发现 search 和 abc 这些都匹配了这个模式串。
那么 * 在正则表达式中又表示什么呢? * 表示重复。
[volcanol@volcanol ~]$ grep -n 'a.*c' regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:motorcycle is cheap than car.
:google is the best tools for search keyword.
:///abc
:////abdddc
可以看到这个模式匹配时,* 将结果放大里很多;因为这里表示将 * 前面的字符串可以重复匹配0到任意次。而 . 在* 前面,他匹配
任意字符,所以有了这个结果。我们还可以看一个别的测试:
[volcanol@volcanol ~]$ grep -n 'bd*c' regular_express.txt
:///abc
:////abdddc
第25行 匹配里 d 零次,而26行匹配里3次。
Exp: 指定重复次数
我们用* 表示重复任意次数,但是有时候我们仅需要重复有限的次数,这时我们可以利用 {} 来表示。但是因为{} 在这里是特殊字符
因此需要用 \ 转义。
[volcanol@volcanol ~]$ grep -n 'bd\{2,\}c' regular_express.txt
:////abdddc
[volcanol@volcanol ~]$ grep -n 'bd\{3,\}c' regular_express.txt
:////abdddc
[volcanol@volcanol ~]$ grep -n 'bd\{4,\}c' regular_express.txt
这里我们可以看到,第一次表示匹配d字符2次或者2次以上,
第二个命令表示匹配d字符3次或者3次以上
第三个命令表示匹配d字符4次或者4次以上
如果我们需要指定重复次数的话可以这样来看:
[volcanol@volcanol ~]$ grep -n 'bd\{2,4\}c' regular_express.txt
:////abdddc
[volcanol@volcanol ~]$ grep -n 'bd\{2,6\}c' regular_express.txt
:////abdddc
上面第一行表示重复2到4次,
第二个命令表示重复2到6次
小结:
在利用grep 这些工具的时候,一定要注意其处理时是支持正则表达式的,要注意与普通工具的差别,否则
将不能得到您想要的结果。
linux_shell_5_shell特性_正则_1的更多相关文章
- C语言编码风格_集锦_1
参考原地址: http://www.jb51.net/article/79257.htm <一> 在一个标准的C语言程序中, 最特殊的莫过于main函数了. 函数大体上分为内联函数(C99 ...
- C# 新特性_协变与逆变 (.net 4.0)
C#4.0中有一个新特性:协变与逆变.可能很多人在开发过程中不常用到,但是深入的了解他们,肯定是有好处的. 协变和逆变体现在泛型的接口和委托上面,也就是对泛型参数的声明,可以声明为协变,或者逆变.什么 ...
- python模块之_正则 re_configparser_logging_hashlib
正则表达式的内容放在最下面了 configparser 模块: #!/usr/bin/env python # coding:utf-8 import configparser # 专门用于操作配置文 ...
- Python笔记_第四篇_高阶编程_正则表达式_1.正则表达式简介(re模块)
1. 从一个判断手机号的问题引入: 如果给你一个字符串,去判断是否是一个手机号码,我们通过之前的学习可以有如下代码: # 如果用普通的方式去检验一个电话号码非常麻烦. def checkPhone(s ...
- 备份、恢复数据库(Dos命令提示符下)_数据库安装工具_连载_1
Dos命令提示符下: 备份.恢复数据库,是不是很简单啊,是的,当你20年不碰MS SQL,是不是又忘记了呢,答案也许也是吧,^_^虽然在程序中执行SQL代码时,很讨厌那个Go,正如MySQL中那个分号 ...
- java 三大特性_继承、封装、多态_day005
一.继承: java的三大特性之一.两个类之间通过extends关键字来描述父子关系,子类便可拥有父类的公共方法和公共属性.子类可以继承父类的方法和属性,子类也可以自己定义没有的方法或者通过覆盖父类的 ...
- Java8新特性_日期时间新类 LocalDate、LocalTime、LocalDateTime
import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeForma ...
- Java8新特性_接口中的默认方法
默认方法由来猜想 1. Collection接口.Collections公共类. 同是操作集合,为啥要搞俩?没必要.在接口中搞一些默认实现,一个接口即搞定了. 2. Java8支持Lambda表达式 ...
- c基础_笔记_1
定义变量 int i; 也可以 int i,num; 赋值,c必须先定义变量再赋值 num = 0; 循环for for(i=1; i<=0; i++) { printf("%d \n ...
随机推荐
- Infinispan 8 中新的 Redis 缓存存储实现
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/147.html nfinispan 8 包含了一个新的在 Redis k/ ...
- MVVM架构~knockoutjs系列之一些异常的总结(永久更新)
返回目录 1 关于attr属性的问题 这个问题主要出现的IE7和360浏览器,使用attr时,需要为属性名加上单引号,代码如下: <a data-bind="attr:{'href': ...
- 在ubuntu上安装nodejs[开启实时web时代]
作为一名菜鸟,竟然在centos桌面上连输入命令行的地方都找不到,是在是对不起开山祖师,最后苍天不负苦心人,在ubuntu上找见了 [安装过程参考了http://cnodejs.org/topic/4 ...
- js 图片轮播(一)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- javascript_basic_03之函数、循环、数组
1.函数:函数(Function),方法(Method),过程(Procedure): 2.默认为假的情况: ①if(0){}:②if(0.0){}:③if(null){}:④if("&qu ...
- Linux常用命令03
上篇我们写到,如何编辑文件,我们有时候,在编辑的时候,有可能会异常的退出,这样的话, linux会针对这个文件生成一个swp文件,当你下次进入vi模式时,就会提示你一个错误 这样,即使你按enter键 ...
- sql基础语句大杂烩
(坑Open Office,这排版...) 1.distinct列出不同值,过滤掉相同的值 例:company中有两个相同的值比如(apple和apple)时,则只取出一个值 SELECT DISTI ...
- javascript中Date对象的应用——简易日历的实现
× 目录 [1]效果 [2]HTML [3]CSS[4]JS 前面的话 简易日历作为javascript中Date对象的常见应用,用途较广泛.本文将详细说明简易日历的实现思路 效果演示 HTML说明 ...
- Android线程机制——AsyncTask
对于Android为什么要使用多线程,因为从Android4.0之后,谷歌规定了网络操作不允许放在主线程中执行,由此就有了多线程的机制,有个JAVA学习经验的朋友一定知道多线程指的是什么,简单来讲就是 ...
- 【C#公共帮助类】 Utils最全的系统帮助类
最近闲的没事做,自己想着做一些东西,不知不觉居然在博客园找到了这么多公共类,感觉还是挺有用的,平时自己还是用到了好多,就是缺少整理,现在为大家分享一下一个Utils系统帮助类,可能有些现在有新的技术替 ...