grep语法格式
egrep 等价于 grep -E
管道方式使用grep 示例 $ cat /etc/passwd | grep "bash"
第一种形式:
grep [option] [pattern] [file1,file2...]
第二种形式
command | grep [option] [pattern]
grep 参数
grep 参数
选项 含义
-v 不显示匹配行信息
-i 搜索时忽略大小写
-n 显示行号
-r 递归搜索
-E 支持拓展正则表达式
-F 不按正则表达式匹配,按照字符串字面意思匹配
-c 显示匹配行总数
-w 匹配整词
-x 匹配整行
-l 显示文件名称不显示内容
-s 不显示错误信息
示例:
$ grep python file
i love python
lovelove python
排除关键字-v
$ grep -v python file
Hello World
忽略大小写 -i
$ grep -i h file
i love python
Hello World
lovelove python
显示行号 -n
$ grep -n h file
1:i love python
3:lovelove python
扩展正则表达式 -E
$ grep -E "python|Hello" file
i love python
Hello World
lovelove python
按照字面意思查找 py.* 不按照正则表达式查找 -F
$ grep -F "py.*" file
$
全文搜索并且显示行号
$ grep love -rn
aaa:1:i love python
aaa:3:lovelove python
file:1:i love python
file:3:lovelove python
匹配行数 -c
$ grep -c love file
2
匹配单词 搜索条件必须前后有空格 -w
l$ grep -w love file
i love python
匹配整行信息 -x
grep -x "i love python" file
i love python
只显示文件名字 -l
$ grep -l love file
file
在输出命令用grep 命令
$ cat /etc/passwd | grep "bash"
root:x:0:0:root:/root:/bin/bash