首页 运维 网络学院 查看内容

linux grep命令详解

2011-5-11 10:50 508 0

摘要:   Grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使...
关键词: nbsp localhost root Grep data 3zl1998 USP KVM Sept 正则

  Grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包括grep、egrep和fgrep。  常用的grep选项有:  -c 只输出匹配行的计数。  -i 不区分大小写(只适用于单字符)。  -h 查询多文件时不显示文件名。  -l 查询多文件时只输出包含匹配字符的文件名。  -n 显示匹配行及行号。  -s 不显示不存在或无匹配文本的错误信息。  -v 显示不包含匹配文本的所有行。  举例说明:  [root@localhost etc]# vi /root/data.f                   #新建data.f文档  48      Dec     3BC1997 LPSX    68.00   LVX2A   138  483     Sept    5AP1996 USP     65.00   LVX2C   189  47      Oct     3zl1998 LPSX    43.00   KVM9D   512  219     dec     2CC1999 CAD     23.00   PLV2C   68  484     nov     7PL1996 CAD     49.00   PLV2C   234  483     may     5PA1998 USP     37.00   KVM9D   644  216     sept    3ZL1998 USP     86.00   KVM9E   234  [root@localhost etc]# grep -c "mzl" /etc/passwd         #行匹配  1  [root@localhost etc]# grep -i "[Ss]ept" /root/data.f    #不区分大小写  483     Sept    5AP1996 USP     65.00   LVX2C   189  216     sept    3ZL1998 USP     86.00   KVM9E   234  [root@localhost etc]# grep -in "[Ss]ept" /root/data.f   #不区分大小写并显示行号  2:483   Sept    5AP1996 USP     65.00   LVX2C   189  7:216   sept    3ZL1998 USP     86.00   KVM9E   234  [root@localhost etc]# grep -v "[Ss]ept" /root/data.f    #显示非匹配行  48      Dec     3BC1997 LPSX    68.00   LVX2A   138  47      Oct     3zl1998 LPSX    43.00   KVM9D   512  219     dec     2CC1999 CAD     23.00   PLV2C   68  484     nov     7PL1996 CAD     49.00   PLV2C   234  483     may     5PA1998 USP     37.00   KVM9D   644  [root@localhost etc]# grep '48\>' /root/data.f          #进行精确匹配  48      Dec     3BC1997 LPSX    68.00   LVX2A   138  grep和正则表达式  使用正则表达式使模式匹配加入一些规则,因此可以在抽取信息中加入更多选择。使用  正则表达式时最好用单引号括起来,这样可以防止grep中使用的专有模式与一些shell命令的特殊方式相混淆。举例说明:  [root@localhost etc]# cd /root  [root@localhost ~]# grep '48[34]' data.f                #模式范围  483     Sept    5AP1996 USP     65.00   LVX2C   189  484     nov     7PL1996 CAD     49.00   PLV2C   234  483     may     5PA1998 USP     37.00   KVM9D   644  [root@localhost ~]# grep '^[^48]' data.f                #不匹配行首  219     dec     2CC1999 CAD     23.00   PLV2C   68  216     sept    3ZL1998 USP     86.00   KVM9E   234  [root@localhost ~]# grep 'K...D' data.f  47      Oct     3zl1998 LPSX    43.00   KVM9D   512  483     may     5PA1998 USP     37.00   KVM9D   644  [root@localhost ~]# grep '[A-Z][A-Z]..C' data.f  483     Sept    5AP1996 USP     65.00   LVX2C   189  219     dec     2CC1999 CAD     23.00   PLV2C   68  484     nov     7PL1996 CAD     49.00   PLV2C   234  [root@localhost ~]# grep '5..199*' data.f               #日期查询  483     Sept    5AP1996 USP     65.00   LVX2C   189  483     may     5PA1998 USP     37.00   KVM9D   644  [root@localhost ~]# grep '[0-9][0-5][0-6]' data.f  47      Oct     3zl1998 LPSX    43.00   KVM9D   512  484     nov     7PL1996 CAD     49.00   PLV2C   234  483     may     5PA1998 USP     37.00   KVM9D   644  216     sept    3ZL1998 USP     86.00   KVM9E   234  [root@localhost ~]# grep '^[0-9][0-5][0-6]' data.f      #模式出现几率  216     sept    3ZL1998 USP     86.00   KVM9E   234  [root@localhost ~]# grep '4\{2,\}' data.f  483     may     5PA1998 USP     37.00   KVM9D   644  [root@localhost ~]# grep -E '219|216' data.f            #匹配“与”或者“或”模式  219     dec     2CC1999 CAD     23.00   PLV2C   68  216     sept    3ZL1998 USP     86.00   KVM9E   234  [root@localhost ~]# grep '^$' data.f                    #空行  [root@localhost ~]# grep -s "mzl" /etc/password  [root@localhost ~]# ps aux | grep portmap  rpc       3595  0.0  0.1   1808   564 ?        Ss   09:20   0:00 portmap  root     14698  0.0  0.1   4628   704 pts/1    R+   14:37   0:00 grep portmap  [root@localhost ~]# ps aux | grep portmap | grep -v "grep"  rpc       3595  0.0  0.1   1808   564 ?        Ss   09:20   0:00 portmap  [root@localhost ~]# STR="hello mzl"  [root@localhost ~]# echo $STR | grep "mzl"             #匹配字符串  hello mzl  [root@localhost ~]# egrep '(3ZL|2CC)' data.f  219     dec     2CC1999 CAD     23.00   PLV2C   68  216     sept    3ZL1998 USP     86.00   KVM9E   234
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部