grep (1) - 打印匹配给定模式的行
命令选项
正则表达式选择与解释:
-E, --extended-regexp PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
-G, --basic-regexp PATTERN 是一个基本正则表达式(缩写为 BRE)
-P, --perl-regexp PATTERN 是一个 Perl 正则表达式
-f, --file=FILE 从 FILE 中取得 PATTERN
-i, --ignore-case 忽略大小写
-w, --word-regexp 强制 PATTERN 仅完全匹配字词
-x, --line-regexp 强制 PATTERN 仅完全匹配一行
-v, --invert-match 选择不匹配的行
--include=PATTERN 仅仅在搜索匹配 PATTERN 的文件时在目录中递归搜索。
--exclude=PATTERN 在目录中递归搜索,但是跳过匹配 PATTERN 的文件。
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
输出控制:
-n, --line-number 输出的同时打印行号
-d, --directories=ACTION 如何处理目录; ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION 如何处理设备, FIFOs and sockets; ACTION is 'read' or 'skip'
-r, --recursive 递归遍历所有目录下的文件,但不包含符号链接.
-R 递归遍历所有目录下的文件,包含符号链接!
-c 不输出匹配的内容,只输出没行内容中匹配的次数.
--color[=WHEN], --colour[=WHEN] WHEN is never, always, or auto.
文件控制:
-B, --before-context=NUM 打印以文本起始的NUM 行
-A, --after-context=NUM 打印以文本结尾的NUM 行
-C, --context=NUM 打印输出文本NUM 行
正则表达式
支持的正则表达式
. 匹配任意单个字符.
[ and ] 匹配集合中的任何一个单个字符.
[^] 匹配指定字符集合以外的字符.
[-] 匹配字符编码中的指定的字符集合.
^ and $ 匹配以什么开始以什么结尾.
\< and \> @Deprecated 分别匹配以空字符串(空格)开头的单词,或者以空字符串(空格)结尾的单词.
\b 匹配单词边界是空字符串(空格)的单词.
\B @Deprecated Matches the empty string provided it's not at the edge of a word.
\w 匹配任意字母和数字.
\W 匹配字母和数字以外的字符.
? 匹配0次或1次.
* 匹配0次或多次.
+ 至少匹配1次.
{n} 匹配n次.
{n,} 至少匹配n次.
{,m} 最多匹配m次.
{n,m} 最少匹配n次,最多匹配m次.
| 或运算.
( and ) 拼接作用,优先级作用.
\n @Deprecated 正则表达式的反向引用.
需要转义的特殊字符(在standard input模式使用,FILE模式不需要!!!)
[ ===> \[ grep -E \[a-z\]* /etc/passwd
] ===> \] grep -E \[a-z\]* /etc/passwd
( ===> \( grep -E \(9\)+ /etc/passwd
) ===> \) grep -E \(9\)+ /etc/passwd
{ ===> \{ grep -E 9\{1,3\} /etc/passwd
} ===> \} grep -E 9\{1,3\} /etc/passwd
\w ===> \\w grep -E \\w /etc/passwd
\W ===> \\W grep -E \\W /etc/passwd
\b ===> \\b man grep | grep -E \\b
\B ===> \\B man grep | grep -E \\B
? ===> ? grep -E 9? /etc/passwd
* ===> * grep -E 9* /etc/passwd
+ ===> + grep -E 9+ /etc/passwd
\< ===> \\\< man grep | grep -E \\\<
\> ===> \\\> man grep | grep -E \\\>
示例
选项的使用
[root@OS7 ~]# grep -n root /etc/passwd
[root@OS7 ~]# grep -i ipaddr /etc/sysconfig/network-scripts/ifcfg-ens32
[root@OS7 ~]# grep -r sysconfig /etc/
[root@OS7 ~]# grep -R sysconfig /etc/
[root@OS7 ~]# grep -nir sysconfig /etc/ | less -i
[root@OS7 ~]# grep -niR sysconfig /etc/ | less -i
[root@OS7 ~]# grep -v root /etc/passwd
[root@OS7 ~]# grep -A3 root /etc/passwd
[root@OS7 ~]# grep -B3 root /etc/passwd
[root@OS7 ~]# grep -C3 root /etc/passwd
正则表达式的使用
[root@OS7 ~]# grep -nE \(.\)* /etc/passwd #匹配所有的字符,不要使用引号标记 \(.\)* ,会失效。
[root@OS7 ~]# man ls | grep \\b #去除所有的空白行
[root@OS7 ~]# man ls | grep \\\< #去除所有的空白行
[root@OS7 ~]# man ls | grep \\\> #去除所有的空白行
[root@OS7 ~]# rpm -ql coreutils | grep -E \(/usr/\)?\(s\)?bin/ #过滤coreutils包中/bin,/sbin,/usr/bin,/usr/sbin包的命令
[root@OS7 ~]# grep -nir --exclude-dir=sysconfig --exclude-dir=selinux sysconfig /etc/ #排除过滤sysconfig和selinux目录
对该命令的思考
一、 grep只能过滤行的内容,不能过滤全文的内容.所以不能使用^和$正则表达式,来过滤文件中的一段内容.只能用来过滤一行中以什么开始,以什么结尾的内容!!!
二、 在使用文件存储正则表达式的时候,是不需要转义特殊的符号的!!!