shell及脚本3——正则表达式
一.正则表达式
1.1. 什么是正则表达式
正则表达式是处理字符串的方法,以行为单位,通过一些特殊符号的辅助,让用户可以轻易进行查找、删除、替换某特定字符串的操作。
1.2. 正则表达式与通配符的区别
网友看法,有些道理,直接摘抄了:
通配符是系统level的,通配符多用在文件名上,比如查找find,ls,cp,等等;
而正则表达式需要相关工具的支持: egrep, awk, vi, perl。在文本过滤工具里,都是用正则表达式,比如像awk,sed等,是针对文件的内容的。不是所有工具(命令)都支持正则表达式。
说白了就是有些命令支持正则表达式,一些不支持。
1.3. 语系对正则表达式的影响
不通语系,对字符的翻译规则不通,例如
LANG=C, 顺序为:0,1,2,3,4....A,B,C,D......Za,b,c,d....z
LANG=zh_CN,顺序为:0,1,2,3,4....a,A,b,B,c,C,d,D......z,Z
1.4. 一些特殊符号
特殊符号可以规避语系的影响,一些常用的特殊符号:
[:alnum:] 所有的字母和数字,0-9,A-Z,a-z
[:alpha:] 所有的字母,A-Z,a-z
[:blank:] 所有呈水平排列的空白字符,空格和TAB
[:cntrl:] 所有的控制字符,CR,LF,TAL,DEL等
[:digit:] 所有的数字,0-9
[:graph:] 所有的可打印字符,不包括空格(空格和TAB)外的所有按键
[:lower:] 所有的小写字母,,a-z
[:print:] 所有的可打印字符,包括空格
[:punct:] 所有的标点字符
[:space:] 所有呈水平或垂直排列的空白字符
[:upper:] 所有的大写字母,A-Z
[:xdigit:] 所有的十六进制数,0-9,A-Z,a-z的数字与字符
二.基础正则表达式
2.1.练习,使用grep
2.1.1. grep的高级功能
grep [-A] [-B] ‘搜索字符串’ filename
-A: after + 数字n,除了该行,列出后面的n行,-An,无空格
-B:before + 数字n,除了该行,列出前面的n行,-Bn,无空格
:/$ dmesg | grep -n 'eth'
:[ 2.427478] e1000 ::01.0 eth0: (PCI:66MHz:-bit) :0c::::
:[ 2.427489] e1000 ::01.0 eth0: Intel(R) PRO/ Network Connection
:[ 2.433153] e1000 ::01.0 ens33: renamed from eth0
:/$ dmesg | grep -n -A3 -B2 'eth' #-A和-B紧接数字,没有空格
-[ 2.364718] Console: switching to colour frame buffer device 100x37
-[ 2.395386] [drm] Initialized vmwgfx 2.9. for ::0f. on minor
:[ 2.427478] e1000 ::01.0 eth0: (PCI:66MHz:-bit) :0c::::
:[ 2.427489] e1000 ::01.0 eth0: Intel(R) PRO/ Network Connection
-[ 2.427823] ahci ::05.0: version 3.0
-[ 2.428781] ahci ::05.0: AHCI 0001.0300 slots ports Gbps 0x3fffffff impl SATA mode
-[ 2.428784] ahci ::05.0: flags: 64bit ncq clo only
:[ 2.433153] e1000 ::01.0 ens33: renamed from eth0
-[ 2.445075] scsi host3: ahci
-[ 2.445243] scsi host4: ahci
-[ 2.445375] scsi host5: ahci
2.1.2. 基础正则表达式练习
使用鸟哥的例子,regular_express.txt
2.1.2.1 查找特定字符串和反向选取
:~/test$ grep -n 'the' regular_express.txt
:I can't finish the test.^M
: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.
:~/test$ grep -vn '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.^M
:GNU is free air not free beer.^M
:Her hair is very beauty.^M
:Oh! The soup taste good.^M
:motorcycle is cheap than car.
:This window is clear.
:Oh! My god!
:The gd software is a library for drafting programs.^M
:I like dog.
:goooooogle yes!
:go! go! Let's go.
2.1.2.2 利用[]查找字符集合
:~/test$ grep -n 't[ae]st' regular_express.txt #[ae]表示1个字符,a或者e
:I can't finish the test.^M
:Oh! The soup taste good.^M
:~/test$ grep -n 'oo' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
:~/test$ grep -n '[^g]oo' regular_express.txt #[^g]不是g,查找oo且前面不是g的,第1行和第9行没有了
:apple is my favorite food.
:Football game is not use feet only.
:google is the best tools for search keyword.
:goooooogle yes!
:~/test$ grep -n '[^a-z]oo' regular_express.txt #找oo且前面不是小写字符的
:Football game is not use feet only.
:~/test$ grep -n '[^[:lower:]]oo' regular_express.txt #[:lower:]小写字符的另一种写法
3:Football game is not use feet only.
:~/test$ grep -n '[0-9]' regular_express.txt #找数字
:However, this dress is about $ dollars.^M
:You are the best is mean you are the no. .
:~/test$ grep -n '[[:digit:]]' regular_express.txt #数字的另一种写法[:digit:]
:However, this dress is about $ dollars.^M
:You are the best is mean you are the no. .
2.1.2.3 行首^与行尾$字符
注意:[^]代表反向选取, 在括号外面^[]表示行首
:~/test$ grep -n '^the' regular_express.txt #找行首是the的
:the symbol '*' is represented as start.
:~/test$ grep -n '^[a-z]' regular_express.txt #行首是小写字符的
:apple is my favorite food.
:this dress doesn't fit me.
:motorcycle is cheap than car.
:the symbol '*' is represented as start.
:google is the best tools for search keyword.
:goooooogle yes!
:go! go! Let's go.
:~/test$ grep -n '^[^a-zA-Z]' regular_express.txt #行首不是字符的
:"Open Source" is a good mechanism to develop programs.
:~/test$ grep -n '\.$' regular_express.txt #行尾是点.的,点前加了转义字符,以为.本身是特殊字符
20:go! go! Let's go.
2.1.2.3 任意一个字符.与重复字符*
*:重复前一个字符到无穷多次的意思,例如a*,代表 “空~无穷多个a”。与通配符不同,通配符中*表示0到多个字符,a*表示a或者“a若干字符”
. : 一定有一个任意字符
:~/test$ grep -n 'g..d' regular_express.txt # g..d表示g和d之间一定有2个任意字符
:"Open Source" is a good mechanism to develop programs.
:Oh! The soup taste good.^M
:The world <Happy> is the same with "glad". :~/test$ grep -n 'ooo*' regular_express.txt #ooo*,表示有~无穷多个o
:"Open Source" is a good mechanism to develop programs.
:apple is my favorite food.
:Football game is not use feet only.
:Oh! The soup taste good.^M
:google is the best tools for search keyword.
:goooooogle yes! :~/test$ grep -n 'g.*g' regular_express.txt # g.*g,找g开头,g结尾的字符,“.*”可以理解成0个或人一多个字符,与通配符中的*相当了
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
:~/test$ grep -n 'g*g' regular_express.txt # g*g,不一定是g开头,g结尾,因为g*表示0~无穷多个g,
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
9:Oh! The soup taste good.^M
13:Oh! My god!
14:The gd software is a library for drafting programs.^M
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
2.1.2.4 限定连续重复的字符范围{}
:~/test$ grep -n 'go\{2,5\}g' regular_express.txt # \{2,5\},2到5个o
:google is the best tools for search keyword.
:~/test$ grep -n 'go\{2\}g' regular_express.txt # \{2\},2个o
:google is the best tools for search keyword.
:~/test$ grep -n 'go\{2,\}g' regular_express.txt # \{2,\},2个及以上个o
:google is the best tools for search keyword.
:goooooogle yes!:~/test$ grep -n 'go\{,5\}g' regular_express.txt # \{,5\},5个及以下个o
:google is the best tools for search keyword.
:~/test$ grep -n 'go\{,10\}g' regular_express.txt # \{,10\},10个及以下个o
:google is the best tools for search keyword.
:goooooogle yes!
2.基础正则表达式字符总结
^word: word在行首
word$: word在行尾
. :一定有一个任意字符
\ :转义
* : 0~无穷多个前一字符
[list] :在list中的1个字符
[n1-n2] :在字符范围内的1个字符
[^lish] :反向选取
\{n1,n2\} : 连续n1到n2个前一字符
3.sed命令
管道命令,可以进行数据替换、删除、新增、选取特定行等。
4.awk命令
按字段处理
5.扩展的正则表达式
+ 重复1个或1个以上前一个字符
? 0个或1个前一个字符
| 或 'glad|good'
() 分组 g(la|oo)d
()+ 1个或多个重复组
shell及脚本3——正则表达式的更多相关文章
- 4.Vim编辑器与Shell命令脚本
第4章 Vim编辑器与Shell命令脚本 章节简述: 本章首先讲解如何使用Vim编辑器来编写.修改文档,然后通过逐个配置主机名称.系统网卡以及Yum软件仓库参数文件等实验,帮助读者加深Vim编辑器中诸 ...
- shell之文本过滤(正则表达式)
shell之文本过滤(正则表达式) 分类: linux shell脚本学习2012-09-14 12:59 213人阅读 评论(0) 收藏 举报 当从一个文件或命令输出中抽取或过滤文本时,可以使用正则 ...
- shell及脚本4——shell script
一.格式 1.1 开头 必须以 "# !/bin/bash" 开头,告诉系统这是一个bash shell脚本.注意#与!中间有空格. 二.语法 2.1 数值运算 可以用decla ...
- 【Telnet】使用Telnet协议连接到远程Shell执行脚本
介绍 本文介绍如何通过Telnet协议连接到远程Shell,执行脚本,并获取执行结果: 相关文章: <[Jsch]使用SSH协议连接到远程Shell执行脚本>http://www.cnbl ...
- shell自动计算脚本
shell自动计算脚本 #!/bin/bash echo $(($)) [root@bogon ~]# sh b.sh 123+123246 let用户声明这个操作是要计算,后者的效率更高 (expr ...
- Shell菜单脚本
今天在这儿给大家分享一个我简单编写的Shell菜单脚本,傻瓜式的人机交互,人人都可以操作linux. #!/bin/sh #Shell菜单演示 function menu () { cat <& ...
- shell常见脚本30例
shell常见脚本30例 author:headsen chen 2017-10-19 10:12:12 本文原素材出自网上,特此申明.有些地方加入我自己的改动 常见的30例shell脚本 1.用 ...
- shell常用脚本
shell常用脚本 author:headsen chen 2017-10-17 15:36:17 个人原创,转载请注明,否则依法追究法律责任 1,vim name.grep.sh 2,cat ...
- 一篇关于Maven项目的jar包Shell启动脚本
使用Maven作为项目jar包依赖的管理,常常会遇到命令行启动,笔者也是哥菜鸟,在做微服务,以及服务器端开发的过程中,常常会遇到项目的启动需要使用main方法,笔者潜心的研究了很多博客,发现大多写的都 ...
随机推荐
- 使用VS2015进行C++开发的6个主要原因
使用VS2015进行C++开发的6个主要原因 使用Visual Studio 2015进行C++开发 在今天的 Build 大会上,进行了“将你的 C++ 代码转移至 VS2015 的 6 个原因”的 ...
- ORACLE实现自定义序列号生成
实际工作中,难免会遇到序列号生成问题,下面就是一个简单的序列号生成函数 (1)创建自定义序列号配置表如下: --自定义序列 create table S_AUTOCODE ( pk1 ) primar ...
- 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)
硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...
- java List的排序
List自定义排序 1.第一种方法,就是list中对象实现Comparable接口,重写compareTo接口, 对排序的字段进行比较.2.第二种方法,就是在重载Collections.sort方法. ...
- HashSet,TreeSet和LinkedHashSet的区别
Set接口Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false.Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就是说,只要两个对象用eq ...
- 由于目标计算机积极拒绝,无法连接。 192.168.1.106:8078 说明: 执行当前 Web 请求期间,出现未经处理的异常。
请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.Net.Sockets.SocketException: 由于目标计算机积极拒绝,无法连接. 1 ...
- SAP CRM 客户控制器与数据绑定
当用户从视图离开时,视图将失去它的数据.解决这个问题,需要引入客户控制器(Custom Controller)(译者注:SAP CRM客户端中,不同地方的Custom Controller会翻译为“客 ...
- VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式
[1]我的IdleTest源码地址:http://idletest.codeplex.com/ [2]IdleTest改动说明:2013年10月份在保持原有功能的情况下对其动了较大的手术,首先将基本的 ...
- Android 防止控件被重复点击
转载: 工具类: public class Utils { private static long lastClickTime; public static boolean isFastDoubleC ...
- 简述AOP编程
aop是面向切面编程的简称,对业务逻辑中的各个部分切割隔离,使耦合度降到最低,不仅增加了开发效率,还增强了系统的重用性和可维护性. 个人理解是把面向对象编程和面向函数编程结合在了一起. 说了这多的好处 ...