grep和egrep 第一种形式:grep [option] [pattern] [file1,file2...] 第二种形式:command | grep [option] [pattern] grep参数 选项 含义
-v 不显示匹配行信息
-i 搜索时忽略大小写
-n 显示行号
-r 递归搜索
-E 支持扩展正则表达式
-F 不按正则表达式匹配,按照字符串字面意思匹配
不常用选项
-c 只显示匹配行总数
-w 匹配整词
-x 匹配整行
-l 只显示文件名,不显示内容
-s 不显示错误信息
grep和egrep:
grep语法格式
grep [option] [pattern] [file1,file2...]
必须掌握的选项:
-v 显示不匹配pattern的行
-i 搜索时忽略大小写
-n 显示行号
-E 支持扩展的正则表达式
-F 不支持正则表达式,按字符串的字面意思进行匹配
-r 递归搜索
需要了解的选项:
-c 只输出匹配行的数量,不显示具体内容
-w 匹配整词
-x 匹配整行
-l 只累出匹配的文件名,不显示具体匹配行内容 grep和egrep:
grep默认不支持扩展正则表达式,只支持基础正则表达式 使用grep -E可以支持扩展正则表达式 使用egrep可以支持扩展正则表达式,与grep -E等价 # 示例 # 显示file中有python的行
[root@es01 ~]# cat file
i love python
hello python
aaa bbb
I LOVE PYTHON
HELLO PYTHON
PYTHON IS A AI LANGUAGE
end [root@es01 ~]# grep python file
i love python
hello python # 没有python的行,不忽略大小写
[root@es01 ~]# grep -v python file
aaa bbb
I LOVE PYTHON
HELLO PYTHON
PYTHON IS A AI LANGUAGE
end # 没有python的行,忽略大小写
[root@es01 ~]# grep -vi python file
aaa bbb
end # 查找/etc/man_db.conf 中带man的行,在文件中的行号
[root@es01 ~]# grep -n man /etc/man_db.conf
:# This file is used by the man-db package to configure the man and cat paths.
:# It is also used to provide a manpath for those without one by examining
:# their PATH environment variable. For details see the manpath() man page.
:# MANDATORY_MANPATH manpath_element
:# MANPATH_MAP path_element manpath_element
:# MANDB_MAP global_manpath [relative_catpath]
:#MANDATORY_MANPATH /usr/src/pvm3/man
:MANDATORY_MANPATH /usr/man
:MANDATORY_MANPATH /usr/share/man
:MANDATORY_MANPATH /usr/local/share/man
:# ie. what man tree holds man pages for what binary directory.
:MANPATH_MAP /bin /usr/share/man
:MANPATH_MAP /usr/bin /usr/share/man
:MANPATH_MAP /sbin /usr/share/man
:MANPATH_MAP /usr/sbin /usr/share/man
:MANPATH_MAP /usr/local/bin /usr/local/man
:MANPATH_MAP /usr/local/bin /usr/local/share/man
:MANPATH_MAP /usr/local/sbin /usr/local/man
:MANPATH_MAP /usr/local/sbin /usr/local/share/man
:MANPATH_MAP /usr/X11R6/bin /usr/X11R6/man
:MANPATH_MAP /usr/bin/X11 /usr/X11R6/man
:MANPATH_MAP /usr/games /usr/share/man
:MANPATH_MAP /opt/bin /opt/man
:MANPATH_MAP /opt/sbin /opt/man
... # -E 支持扩展正则表达式选项
[root@es01 ~]# cat file
i love python
hello python
aaa bbb
I LOVE PYTHON
HELLO PYTHON
PYTHON IS A AI LANGUAGE
end [root@es01 ~]# grep "python|PYTHON" file
[root@es01 ~]# grep -E "python|PYTHON" file
i love python
hello python
I LOVE PYTHON
HELLO PYTHON
PYTHON IS A AI LANGUAGE
[root@es01 ~]# # 只显示匹配到的行数
[root@es01 ~]# grep -c python file [root@es01 ~]# grep -c man /etc/man_db.conf # 匹配python或者PYTHON
[root@es01 ~]# grep -E "python|PYTHON" file
i love python very
hello python
I LOVE PYTHON
HELLO PYTHON
PYTHON IS A AI LANGUAGE
lovelove python
[root@es01 ~]# egrep "python|PYTHON" file
i love python very
hello python
I LOVE PYTHON
HELLO PYTHON
PYTHON IS A AI LANGUAGE
lovelove python