grep, egrep, fgrep -
print lines matching a pattern
grep [-cinrvABC] 'word' filename
# grep nologin passwd | head -5
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
-c 行数
# grep nologin passwd -c | head -5
16
-i 不区分大小写
# grep nologin passwd -i | head -5
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/NOLOGIN
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
-n 显示行号
# grep nologin passwd -n | head -5
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
-v 取反
# grep nologin passwd -v | head -5
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/NOLOGIN
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
-r 遍历所有子目录
# grep -r PS /etc/profile.d/
/etc/profile.d/bash_completion.sh:[ -z "$BASH_VERSION" -o -z "$PS1" -o -n "$BASH_COMPLETION_COMPAT_DIR" ] && return
--exclude=GLOB
Skip files whose base name matches GLOB (using wildcard matching).
A file-name glob can use *, ?, and [...] as wildcards,
and \ to quote a wildcard or backslash character literally.
--exclude-from=FILE
Skip files whose base name matches any of the file-name globs read from FILE
--exclude-dir=DIR
Exclude directories matching the pattern DIR from recursive searches.
--include=GLOB
Search only files whose base name matches GLOB
# grep -r --include="*.php" 'eval' /data/
-A 后面跟数字,过滤出符合要求的行以及下面n行
# grep root passwd -n -A 2
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/NOLOGIN
--
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
-B 同上,过滤出符合要求的行以及上面n行
# grep root passwd -n -B 2
1:root:x:0:0:root:/root:/bin/bash
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
-C 同上,同时过滤出符合要求的行以及上下各n行
# grep root passwd -n -C 2
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/NOLOGIN
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
示例:
grep '[0-9]' /etc/inittab
# grep '[0-9]' /etc/inittab
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
grep -v '[0-9]' /etc/inittab
grep -v '^#' /etc/inittab
grep -v '^#' /etc/inittab | grep -v '^$'
grep '^[^a-zA-Z]' test.txt
[^] 在此表示取反,
例如 [^0-9] 那就是非数字(包括字母+特殊符号)
例如[^a-zA-Z] 那就是非字母(包括数字+特殊符号)
例如[^0-9a-zA-Z]那就是非数字字幕(特殊符号)
[^] 匹配非方括号里面的内容,只要不是方括号里面的内容都匹配。
^[^] 匹配非方括号里面的内容开头的所有字符。
grep 'r.o' test.txt
. 匹配任意一个字符
grep 'oo*' test.txt
o* 表示o重复n次,n>=0
grep '.*' test.txt
.* 表示任意一个字符重复任意次,包括0次,即通配
grep 'o\{2\}' /etc/passwd
o\{n\} 匹配n个o
grep 'o\{2,4\}' passwd
'o\{m,n\}' 匹配m~n个o,m<=n, m>=0
egrep 'o{2}' /etc/passwd
egrep is the same as grep -E.
-E, --extended-regexp
Interpret PATTERN as an
extended regular expression (ERE).
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern.
egrep '(oo){2}' /etc/passwd
()中的字符将视作一个整体被匹配
egrep 'o+' /etc/passwd
o+ 表示o重复n次,n>=1
egrep 'oo?' /etc/passwd
o? 表示o重复0次或一次
egrep 'root|nologin' /etc/passwd
| 表示 or
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.
正则表达式_百度百科
等价:
等价是等同于的意思,表示同样的功能,用不同符号来书写。
?,*,+,\d,\w 都是等价字符
?等价于匹配长度{0,1}
*等价于匹配长度{0,}
+等价于匹配长度{1,}
\d等价于[0-9]
\D等价于[^0-9]
\w等价于[A-Za-z_0-9]
\W等价于[^A-Za-z_0-9]。
常用运算符与表达式:
^ 开始
() 域段
[] 包含,默认是一个字符长度
[^] 不包含,默认是一个字符长度
{n,m} 匹配长度
. 任何单个字符(\. 字符点)
| 或
\ 转义
$ 结尾
[A-Z] 26个大写字母
[a-z] 26个小写字母
[0-9] 0至9数字
[A-Za-z0-9] 26个大写字母、26个小写字母和0至9数字
, 分割
分割语法:
[A,H,T,W] 包含A或H或T或W字母
[a,h,t,w] 包含a或h或t或w字母
[0,3,6,8] 包含0或3或6或8数字
语法与释义:
基础语法 "^([]{})([]{})([]{})$"
正则字符串 = "开始([包含内容]{长度})([包含内容]{长度})([包含内容]{长度})结束"
?,*,+,\d,\w 这些都是简写的,完全可以用[]和{}代替,在(?:)(?=)(?!)(?<=)(?<!)(?i)(*?)(+?)这种特殊组合情况下除外。
初学者可以忽略?,*,+,\d,\w一些简写标示符,学会了基础使用再按表自己去等价替换
实例:
字符串;tel:086-0666-88810009999
原始正则:"^tel:[0-9]{1,3}-[0][0-9]{2,3}-[0-9]{8,11}$"
速记理解:开始 "tel:普通文本"[0-9数字]{1至3位}"-普通文本"[0数字][0-9数字]{2至3位}"-普通文本"[0-9数字]{8至11位} 结束"
等价简写后正则写法:"^tel:\d{1,3}-[0]\d{2,3}-\d{8,11}$" ,
简写语法不是所有语言都支持
。
可以使用方括号来指定多个字符区间。例如正则表达式[A-Za-z]匹配任何字母,包括大写和小写的;正则表达式[A-Za-z][A-Za-z]* 匹配一个字母后面接着0或者多个字母(大写或者小写)。当然我们也可以用
元字符
+做到同样的事情,也就是:[A-Za-z]+ ,和[A-Za-z][A-Za-z]*完全等价。
但是要注意元字符+ 并不是所有支持正则表达式的程序都支持的。
关于这一点可以参考后面的正则表达式语法支持情况。
正则表达式语法支持情况
要指定特定数量的匹配,要使用大括号(注意必须使用
反斜杠
来转义)。想匹配所有10和100的实例而排除1和 1000,可以使用:10\{1,2\},这个正则表达式匹配数字1后面跟着1或者2个0的模式。在这个元字符的使用中一个有用的变化是忽略第二个数字,例如正则表达式0\{3,\} 将匹配至少3个连续的0。