bash grep 正则

三剑客 Liemer_Lius 295℃

涉及重复的元字符

Repetition: A regular expression may be followed by one of several repetition operators:
?      The preceding item is optional and matched at most once.
*      The preceding item will be matched zero or more times.
+      The preceding item will be matched one or more times.
{n}    The preceding item is matched exactly n times.
{n,}   The preceding item is matched n or more times.
{,m}   The preceding item is matched at most m times.  This is a GNU extension.
{n,m}  The preceding item is matched at least n times, but not more than m times.

与通配的联系:

*: 匹配任意长度的任意字符
?:匹配任意单个字符
[]:匹配指定范围内的任意单个字符
[abc], [a-m], [a-z], [A-Z], [0-9], [a-zA-Z], [0-9a-zA-Z]
[:space:]:空白字符
[:punct:]:标点符号
[:lower:]:小写字母
[:upper:]: 大写字母
[:alpha:]: 大小写字母
[:digit:]: 数字
[:alnum:]: 数字和大小写字母
注意, 如上的部分表示的是一个范围, 而如果和[]联合使用, 以匹配单个字符的话, 需要在外面再包一层[]:
[[:space:]] ... ...

例题:Click

# 过滤sql-pipeline-v2.0开关,中间多个数字、字母、_、/、.,以.sh结尾的串
egrep -o 'sql-pipeline-v2.0([[:alnum:]]|_|-|/|\.){1,}\.sh' /tmp/list.txt

 

转载请注明:liutianfeng.com » bash grep 正则

喜欢 (8)

评论已关闭。