您的位置:首页 > 运维架构 > Shell

bash正则表达式

2017-04-15 00:00 239 查看

一、正则表达式与通配符

在bash中正则表达式和通配符是两个不一样的概念,甚至同一个符号在正则表达式和通配符中所表达的意义也是完全不同的



二、基础正则表达式



2.1 “*”匹配符



###找出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 “.”匹配符



###匹配c和f之间有两个字符的行
[root@wenhaijin tmp]# grep "c..f" --color  redis.conf

得到以下结果



###匹配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 “^”匹配行首,"$"匹配行尾



###匹配以小写"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 "[]"匹配中括号中指定的任意一个字符



###匹配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




2.5 “\”转义字符



###匹配以英文句号结尾的行,结果太多不黏进来,如果没有"\"则匹配以任意一个字符结尾的行
[root@wenhaijin tmp]# grep "\.$" redis.conf




###匹配连续出现三个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]#




###匹配至少出现连续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]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息