grep是linux中的文本过滤工具,命令格式为grep [选项] PATTERN [文件],功能是grep
按行检索输入的每一行,如果输入行包含模式PATTERN
,则输出这一行,pattern可以是正则表达式
1. 从/etc/passwd文件中查询包含root的行
root@iZuf6ic9ggky8ivrx52hxvZ:~# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
2. 从标准输入中查询包含root的行
root@iZuf6ic9ggky8ivrx52hxvZ:~# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
3. 从/etc/passwd和/etc/group中查询以root开头的行,注意正则表达式一定要用双引号
root@iZuf6ic9ggky8ivrx52hxvZ:~# grep "^root" /etc/passwd /etc/group
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/group:root:x:0:
4. 输出文件/etc/passwd中以/bin/bash结尾的行
root@iZuf6ic9ggky8ivrx52hxvZ:~# grep "/bin/bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
5. 类似的正则表达式模式串
6. 可选参数
-i
使grep在匹配模式时忽略大小写-o
表示只输出匹配的字符,而不是整行-c
统计匹配的行数-v
表示取反匹配-f FILE
表示以文件FILE中的每一行作为模式匹配-w
表示匹配整个单词-n
表示显示行号
参考:
1. https://www.cnblogs.com/peida/archive/2012/12/17/2821195.html