bash正则表达式
2017-04-15 00:00
239 查看
一、正则表达式与通配符
在bash中正则表达式和通配符是两个不一样的概念,甚至同一个符号在正则表达式和通配符中所表达的意义也是完全不同的![](https://static.oschina.net/uploads/space/2017/0415/220953_v1Ab_2988360.png)
二、基础正则表达式
![](https://static.oschina.net/uploads/space/2017/0416/104525_FLk6_2988360.png)
2.1 “*”匹配符
![](https://static.oschina.net/uploads/space/2017/0416/104307_oC1I_2988360.png)
###找出redis.conf文件中含有0到多个a的行(不要理解成含有a或以a打头的行),该正则无任何意义,实际上是列出所有内容 [root@wenhaijin tmp]# grep "a*" redis.conf ###找出内容中含有redi,后面接0或多个s的行 [root@wenhaijin tmp]# grep "redis*" redis.conf # ./redis-server /path/to/redis.conf # unixsocket /tmp/redis.sock pidfile /var/run/redis_6379.pid # others with access to the host running redis-server. [root@wenhaijin tmp]# ###找出包含至少有两个连续t的行,并高亮显示tt(工具原因,这边高亮没发展示出来) [root@wenhaijin tmp]# grep "ttt*" redis.conf --color auto # to customize a few per-server settings. Include files can include # Notice option "include" won't be rewritten by command "CONFIG REWRITE" # line as value of a configuration directive, you'd better put includes # options, it is better to use include as the last line. # synchronization". An RDB file is transmitted from the master to the slaves. # works better. # repl-backlog-ttl 3600 # A slave with a low priority number is considered better for promotion, so # Setting one or the other to 0 disables the feature. # A Redis master is able to list the address and port of the attached # Warning: since Redis is pretty fast an outside user can try up to # AOF file or transmitted to slaves may cause problems. # WARNING: If you have slaves attached to an instance with maxmemory on, # In short... if you have slaves attached it is suggested that you set a lower # volatile-ttl -> remove the key with the nearest expire time (minor TTL) # Both the hard or the soft limit can be disabled by setting them to zero.
2.2 “.”匹配符
![](https://static.oschina.net/uploads/space/2017/0417/221940_Zvjc_2988360.png)
###匹配c和f之间有两个字符的行 [root@wenhaijin tmp]# grep "c..f" --color redis.conf
得到以下结果
![](https://static.oschina.net/uploads/space/2017/0417/222601_f3Rn_2988360.png)
###匹配featur和e之间有任意字符的行 [root@wenhaijin tmp]# grep "featur.*e" redis.conf # Setting one or the other to 0 disables the feature. # By default min-slaves-to-write is set to 0 (feature disabled) and [root@wenhaijin tmp]#
2.3 “^”匹配行首,"$"匹配行尾
![](https://static.oschina.net/uploads/space/2017/0417/223609_ezAp_2988360.png)
###匹配以小写"z"打头的行 [root@wenhaijin tmp]# grep "^z" redis.conf zset-max-ziplist-entries 128 zset-max-ziplist-value 64 [root@wenhaijin tmp]# ###匹配以小写"m"结尾的行 [root@wenhaijin tmp]# grep "m$" redis.conf # The lag in seconds, that must be <= the specified value, is calculated from # volatile-lru -> remove the key with an expire set using an LRU algorithm # allkeys-lru -> remove any key according to the LRU algorithm [root@wenhaijin tmp]# ###匹配空白行 -n显示行号 [root@wenhaijin tmp]# grep -n "^$" redis.conf 7: 19: 21: 37: 321: 323: 326: 330: 335: [root@wenhaijin tmp]#
2.4 "[]"匹配中括号中指定的任意一个字符
![](https://static.oschina.net/uploads/space/2017/0417/225626_pfJI_2988360.png)
###匹配Bath或者Both或者Buth [root@wenhaijin tmp]# grep "B[aou]th" redis.conf # Both the hard or the soft limit can be disabled by setting them to zero. # Bath the hard or the soft limit can be disabled by setting them to zero. # Buth the hard or the soft limit can be disabled by setting them to zero. [root@wenhaijin tmp]# ###匹配包含任意数字的行(行数太多,不黏进来) [root@wenhaijin tmp]# grep "[0-9]" redis.conf ###匹配以任意数字开头的行 [root@wenhaijin tmp]# grep "^[0-9]" redis.conf [root@wenhaijin tmp]# ###如果^在中括号里面则表示取反 ###显示不是以数字打头的行,(行数太多,不黏进来) [root@wenhaijin tmp]# grep "^[^0-9]" redis.conf
![](https://static.oschina.net/uploads/space/2017/0417/225700_lTzh_2988360.png)
2.5 “\”转义字符
![](https://static.oschina.net/uploads/space/2017/0417/230001_EW3D_2988360.png)
###匹配以英文句号结尾的行,结果太多不黏进来,如果没有"\"则匹配以任意一个字符结尾的行 [root@wenhaijin tmp]# grep "\.$" redis.conf
![](https://static.oschina.net/uploads/space/2017/0417/230553_MN8P_2988360.png)
###匹配连续出现三个i的行 [root@wenhaijin tmp]# grep "i\{3\}" redis.conf # biiig latency spikes. [root@wenhaijin tmp]# ###匹配至少有5个连续数字的行 [root@wenhaijin tmp]# grep "[0-9]\{5\}" redis.conf # 1m => 1000000 bytes # 1g => 1000000000 bytes # Accept connections on the specified port, default is 6379 (IANA #815344). save 60 10000 # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # this limit is set to 10000 clients, however if the Redis server is not # maxclients 10000 [root@wenhaijin tmp]#
![](https://static.oschina.net/uploads/space/2017/0417/231030_gsZm_2988360.png)
###匹配至少出现连续5个数字的行 [root@wenhaijin tmp]# grep "[0-9]\{5,\}" redis.conf # 1m => 1000000 bytes # 1g => 1000000000 bytes # Accept connections on the specified port, default is 6379 (IANA #815344). save 60 10000 # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # this limit is set to 10000 clients, however if the Redis server is not # maxclients 10000 [root@wenhaijin tmp]# ###列出最少连续4个数字,最多连续5个数字的行 [root@wenhaijin tmp]# grep "[0-9]\{4,5\}" redis.conf # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 bytes # 1g => 1000000000 bytes # 1gb => 1024*1024*1024 bytes # Accept connections on the specified port, default is 6379 (IANA #815344). port 6379 pidfile /var/run/redis_6379.pid save 60 10000 # repl-backlog-ttl 3600 # slave-announce-port 1234 # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # this limit is set to 10000 clients, however if the Redis server is not # maxclients 10000 [root@wenhaijin tmp]# ###列出b和g之间最少2个i,最多3个i的行 [root@wenhaijin tmp]# grep "bi\{2,3\}g" redis.conf # biiig latency spikes. [root@wenhaijin tmp]#
相关文章推荐
- bash正则表达式部分知识点
- Bash之正则表达式
- Linux上的文件管理类命令和bash 的基础特性及正则表达式
- Bash 脚本:正则表达式基础篇
- bash编程-正则表达式
- 【FAQ】bash VS dash 字符串正则表达式匹配
- bash 正则表达式匹配,一行文本中 “包含 ABC” 并且 “不包含 XYZ”
- bash中正则表达式
- bash通配符和正则表达式
- bash 正则表达式习题
- Bash 脚本:正则表达式基础篇
- [bash]正则表达式、BRE模式
- bash通配符 shell正则表达式
- bash之正则表达式
- BASH系列(3)―― BASH通配符和正则表达式
- bash 中的正则表达式
- 3、输入输出重定向、bash算术、正则表达式 学习笔记
- Bash 脚本:正则表达式基础篇 | Linux 中国
- linux bash shell 正则表达式
- bash学习之九:正则表达式(regular expression)和grep