grep命令详解

简介:

linux中的grep命令主要用于全局搜索,grep家族有三个:grepegrepfgerp其中fgerp不支持正则表达式。如果需要搜素字符串,不需要使用元字符,可以用fgerp比较快,grep  文本过滤工具或文本匹配工具,能够实现根据指定的模式逐行搜索文件内容,并将匹配到的行显示出来。并不一定是全部匹配。可以与正则表达式的元字符,其他字符组成的匹配条件。

 

1、命令格式:

 

grep  [option] … ‘PATTERN’  文件名…………….    

[option]选项:
--color :显示颜色。
-v :显示没有被模式匹配到的行,取反。
-o :只显示被模式匹配到的字符串。
-i :不考虑大小写。
-n :顺便输出行号。

 

2、基本应用

 

[root@localhost ~]# grep -o "root" /etc/passwd 
[root@localhost ~]# grep 'dwzhang' /etc/passwd 
[root@localhost ~]# grep "\$?" ypbind 
[root@localhost ~]# grep 'dwzhang' /etc/passwd
[root@localhost ~]# grep 'cpu' /proc/cpuinfo   //只显示包含cpu的字符
[root@localhost ~]# grep --color=auto  'cpu' /proc/cpuinfo   
[root@localhost ~]# alias grep='grep --color'      
[root@localhost ~]# grep 'dwzhang' /etc/passwd
[root@localhost ~]# ifconfig | grep 'inet addr'   //只显示inet addr

3、结合cut搜索

 

[root@localhost ~]# grep 'dwzhang' /etc/passwd | cut -d: -f1
[root@localhost ~]# grep 'dwzhang' /etc/passwd | cut -d: -f2
[root@localhost ~]# ifconfig | grep 'inet addr' | cut -d: -f1
[root@localhost ~]# ifconfig | grep 'inet addr' | cut -d: -f2
[root@localhost ~]# ifconfig | grep 'inet addr' | cut -d: -f2 | cut -d' ' -f1

 

4、结合正则表达式元字符搜索

[root@localhost ~]# grep 'c.u' /proc/cpuinfo   //匹配c什么p,单个字符
[root@localhost ~]# grep --color 'c..' /proc/cpuinfo   
[root@localhost ~]# grep 'r..t' /etc/passwd
[root@localhost ~]# grep 'c..[a-z]' /proc/cpuinfo  //跟了任意2个字符,后面跟了a-z小写字母的
[root@localhost ~]# grep '^i' /etc/inittab  //i开头的行
[root@localhost ~]# grep 'w$' /etc/inittab  //行尾为w的行
[root@localhost ~]# grep 'b..h$' /etc/passwd   //b什么h结尾的
[root@localhost ~]# grep '\([0-9]\).*\1$' inittab
[root@localhost ~]# grep '^\([0-9]\).*\1$' inittab     
[root@localhost ~]# grep '^fpu' /proc/cpuinfo  //显示匹配的fpu开头的行
[root@localhost ~]# grep -A 2 '^fpu' /proc/cpuinfo  //显示匹配的行和下面的2
[root@localhost ~]# grep --color -B 2 '^fpu' /proc/cpuinfo  //显示匹配的行和上面的2
[root@localhost ~]# grep --color -C 2 '^fpu' /proc/cpuinfo //显示匹配的行和上下面的2   linux中的grep命令主要用于全局搜索,grep家族有三个:grepegrepfgerp其中fgerp不支持正则表达式。如果需要搜素字符串,不需要使用元字符,可以用fgerp比较快,grep  文本过滤工具或文本匹配工具,能够实现根据指定的模式逐行搜索文件内容,并将匹配到的行显示出来。并不一定是全部匹配。可以与正则表达式的元字符,其他字符组成的匹配条件。

 

1、命令格式:

 

grep  [option] … ‘PATTERN’  文件名…………….    

[option]选项:
--color :显示颜色。
-v :显示没有被模式匹配到的行,取反。
-o :只显示被模式匹配到的字符串。
-i :不考虑大小写。
-n :顺便输出行号。

 

2、基本应用

[root@localhost ~]# grep -o "root" /etc/passwd 
[root@localhost ~]# grep 'dwzhang' /etc/passwd 
[root@localhost ~]# grep "\$?" ypbind 
[root@localhost ~]# grep 'dwzhang' /etc/passwd
[root@localhost ~]# grep 'cpu' /proc/cpuinfo   //只显示包含cpu的字符
[root@localhost ~]# grep --color=auto  'cpu' /proc/cpuinfo   
[root@localhost ~]# alias grep='grep --color'      
[root@localhost ~]# grep 'dwzhang' /etc/passwd
[root@localhost ~]# ifconfig | grep 'inet addr'   //只显示inet addr

3、结合cut搜索

[root@localhost ~]# grep 'dwzhang' /etc/passwd | cut -d: -f1
[root@localhost ~]# grep 'dwzhang' /etc/passwd | cut -d: -f2
[root@localhost ~]# ifconfig | grep 'inet addr' | cut -d: -f1
[root@localhost ~]# ifconfig | grep 'inet addr' | cut -d: -f2
[root@localhost ~]# ifconfig | grep 'inet addr' | cut -d: -f2 | cut -d' ' -f1

 

4、结合正则表达式元字符搜索

[root@localhost ~]# grep 'c.u' /proc/cpuinfo   //匹配c什么p,单个字符
[root@localhost ~]# grep --color 'c..' /proc/cpuinfo   
[root@localhost ~]# grep 'r..t' /etc/passwd
[root@localhost ~]# grep 'c..[a-z]' /proc/cpuinfo  //跟了任意2个字符,后面跟了a-z小写字母的
[root@localhost ~]# grep '^i' /etc/inittab  //i开头的行
[root@localhost ~]# grep 'w$' /etc/inittab  //行尾为w的行
[root@localhost ~]# grep 'b..h$' /etc/passwd   //b什么h结尾的
[root@localhost ~]# grep '\([0-9]\).*\1$' inittab
[root@localhost ~]# grep '^\([0-9]\).*\1$' inittab     
[root@localhost ~]# grep '^fpu' /proc/cpuinfo  //显示匹配的fpu开头的行
[root@localhost ~]# grep -A 2 '^fpu' /proc/cpuinfo  //显示匹配的行和下面的2
[root@localhost ~]# grep --color -B 2 '^fpu' /proc/cpuinfo  //显示匹配的行和上面的2
[root@localhost ~]# grep --color -C 2 '^fpu' /proc/cpuinfo //显示匹配的行和上下面的2

 

例子:
1、显示文件去除掉空白行:
[root@localhost ~]# grep -v "^$" /etc/httpd/conf/httpd.conf
2、显示文件去除掉#注释行:
[root@localhost ~]# grep -v "#" /etc/httpd/conf/httpd.conf
3、显示去除掉注释行和空白行:
[root@localhost ~]# grep -v "#" /etc/httpd/conf/httpd.conf | grep -v "^$"

 

例子:
1、显示文件去除掉空白行:
[root@localhost ~]# grep -v "^$" /etc/httpd/conf/httpd.conf
2、显示文件去除掉#注释行:
[root@localhost ~]# grep -v "#" /etc/httpd/conf/httpd.conf
3、显示去除掉注释行和空白行:
[root@localhost ~]# grep -v "#" /etc/httpd/conf/httpd.conf | grep -v "^$"

















本文转自信自己belive51CTO博客,原文链接:  http://blog.51cto.com/11638205/1917198 ,如需转载请自行联系原作者

相关文章
|
2月前
|
Unix Linux Shell
linux命令之grep
linux命令之grep
35 2
|
网络协议 Unix 网络安全
|
数据安全/隐私保护
|
Linux Shell 数据安全/隐私保护
|
应用服务中间件 Perl JavaScript