Grep命令详解-9个经典使用场景

Grep

全称Global Regular Expression Print,表示全局正则表达式

是一个强大的文本搜索工具,采用正则匹配

1、命令格式

grep [options] files

2、主要参数

-c: 只输出匹配行的数目

-i: 不区分大小写

-n:显示匹配航以及行号

-l:查询多文件的时候只输出包含匹配字符的文件名

-v:反向匹配,即显示不匹配的行

-h: 查询的时候不适用文件名

-s:不显示错误信息

3、部分正则表达式

\     反义字符:如"\"\""表示匹配""

^$ 开始和结束

[] 单个字符,[A]

[ - ] 匹配一个范围,[0-9a-zA-Z]匹配所有数字和字母

* 前面的字符出现0次或者多次

+ 前面的字符出现了一次或者多次

. 任意字符

4、经典场景

除非要精确区分大小写,否则请加上-i来忽略大小写

(1)结合find命令和管道

你的一个音乐文件夹里有多种格式的文件,而你只想找到艺术家jay的mp3文件,并且不含有任何的混合音轨

[[email protected] ~]#find . -name ".mp3" | grep -i jay | grep -vi "remix"

分析: 1)使用find -name 来列出所有mp3文件,重定向给grep

2) 使用grep -i 来查找包含jay的行

3)使用grep -vi 来查找不包含remix的行

(2)-A -B -C

很多时候,我们并关心匹配行而是关心匹配行的上下文。这时候-A -B -C就有用了

-A n 后n行,A记忆为(After)

-B n 前n行,B记忆为(Before)

-C n 前n行,后n行,C记忆为(Center)

举例

[[email protected] ~]# ifconfig | grep -A 2 "Link encap"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F3:38:15
          inet addr:192.168.91.129  Bcast:192.168.91.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fef3:3815/64 Scope:Link
--
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host

[[email protected] ~]#  ifconfig | grep -C 2 "lo"
          Interrupt:67 Base address:0x2024 

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host

(3) 用-c来统计数目

你手头有一个很大的文件,这个文件包含网址,比如www.baidu.com tieba.baidu.com等等。你想要知道有多少个隶属于百度的网址

[

[email protected] ~]# grep -c "*baidu.com*" filename
例子
[[email protected] ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
[email protected]
[email protected] 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[[email protected] ~]# grep -cn ".*baidu.com.*" file.txt
3

(4) -r 递归查找子目录

查找当前目录极其子目录下面包含匹配字符的文件

查找子目录,匹配后输出行号,这里的点表示当前目录

[[email protected] ~]# grep -nr HELLO_HWC_CSND_BLOG* .

例子:

[[email protected] ~]# grep -nr baidu .
./file.txt:8:www.baidu.com
./file.txt:9:tieba.baidu.com
./file.txt:11:www.baidu.com/search/index
./test/test.txt:1:http://www.baidu.com

查找子目录,匹配后只输出文件名

[[email protected] ~]# grep -lr HELLO_HWC_CSND_BLOG* .

例子:

[[email protected] ~]# grep -lr baidu .
./file.txt
./test/test.txt

(5)--line-buffered 打开buffering 模式

你有一个文件是动态的,它不断地添加信息到文件的尾部,而你想要输出包含某些信息的行。即持续的grep一个动态的流

[[email protected] ~]#tail -f file | grep --line-buffered your_pattern

(6)结合ps查找进程

[[email protected] ~]# ps aux | grep init
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
root      4956  0.0  0.1   3920   680 pts/1    R+   23:27   0:00 grep init

这里我们看到了grep init我们执行的命令也被列出来了

如果不想要这一行,我们可以这么改命令

[[email protected] ~]# ps aux | grep [i]nit
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients

(7)查找不包含某一个目录

[[email protected] ~]#grep -R --exclude-dir=node_modules ‘some pattern‘ /path/to/search

例子

[[email protected] ~]# ls
anaconda-ks.cfg  Desktop  file.txt  find.result  install.log  install.log.syslog  test
[[email protected] ~]# grep -r baidu .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
./test/test.txt:http://www.baidu.com

这时候如果我们不想包含test目录

[[email protected] ~]# grep -R --exclude-dir=text "baidu" .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index

如果报错

grep: unrecognized option `--exclude-dir=test'

说明版本过老,更新下就ok

(8)查找IP地址

这里用到了-o和-P命令

我们通过man grep查看

-o, --only-matching:

Show only the part of a matching line that matches PATTERN.

-P, --perl-regexp:

Interpret PATTERN as a Perl regular expression.

也就是说-o,只显示匹配行中匹配正则表达式的那部分

-P,作为Perl正则匹配

[[email protected] ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
[email protected]
[email protected] 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[[email protected] ~]# grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt
192.168.0.1
162.12.0.123

(9)查找邮箱

[[email protected] ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt

例子

[[email protected] ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
[email protected]
[email protected] 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[[email protected] ~]# grep -oP "[a-zA-Z0-9_-][email protected][a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt
[email protected]
[email protected]

时间: 2024-08-09 21:44:22

Grep命令详解-9个经典使用场景的相关文章

文本处理工具之一grep命令详解

grep(Globel Search Regular Expression and Printing out the line)全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,是一个对行进行操作的搜索工作,它能使用正则表达式搜索文本,并把匹配的行打印出来.Unix的grep家族包括grep.egrep和fgrep. egrep表示扩展的grep,相比grep支持更多的元字符,"grep -E"相当于egrep.fgrep是fast grep,不支持元字符,但是搜索速度更快.

grep命令详解

grep命令是linux下的行过滤工具,其参数繁多,下面就一一介绍个个参数的作用,希望对大家有所帮助.grep -- print lines matching a pattern (将符合样式的该行列出)◎语法: grep [options] PATTERN [FILE...] grep用以在file内文中比对相对应的部分,或是当没有指定档案时, 由标准输入中去比对. 在预设的情况下,grep会将符合样式的那一行列出.此外,还有两个程序是grep的变化型,egrep及fgrep. 其中egrep

grep 命令详解及相关事例

一.匹配字符 .    匹配任意单个字符 [ ]  匹配指定范围内的任意字符 [^]  匹配飞指定范围内的任意字符 [:alpha:] 字母字符 [:lower:] 小写字母字符 [:upper:] 大写字母字符 [:digit:] 数字 [:alnum:] 字母数字字符 [:space:] 空白字符(禁止打印),如回车符.换行符.竖直制表符和换页符 [:punct:] 标点字符 [:cntrl:] 控制字符(禁止打印) [:print:] 可打印字符 使用时一般使用两个中括号,具体会在下面的例

linux grep命令详解

linux grep命令详解 http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. Unix的grep家族包括grep.egrep和fgrep.egrep和fgrep的命令只跟gr

shell --- grep 命令详解

一.grep  ---- "行过滤工具" grep ( global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来 )是一种强大的文本搜索工具,它能使用正则表达式搜索文本,查找包含某些字符串的行或符合某个模式(Pattern)的一类字符串的行,并把匹配的行打印出来.   grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则

【转】grep命令详解

原文链接  http://bbs.chinaunix.net/thread-1810167-1-1.html grep命令是linux下的行过滤工具,其参数繁多,下面就一一介绍个个参数的作用,希望对大家有所帮助. grep -- print lines matching a pattern (将符合样式的该行列出) ◎语法: grep [options] PATTERN [FILE...] grep用以在file内文中比对相对应的部分,或是当没有指定档案时, 由标准输入中去比对. 在预设的情况下

Linux入门基础之grep命令详解及正则表达式

grep命令是linux下经常使用的命令之一,能根据用户指定的模式(pattern)对文本进行过滤,显示出匹配到的行.其命令格式为: grep [OPTIONS] PATTERN [FILE] 例如:我们要查找网卡0中配置的IP地址(该文件路径: /etc/sysconfig/network-scripts/ifcfg-eth0)---grep 'IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 (注:alias grep='grep --col

Linux的grep命令详解

简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. Unix的grep家族包括grep.egrep和fgrep.egrep和fgrep的命令只跟grep有很小不同.egrep是grep的扩展,支持更多的re元字符, fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也

Shell编程之---grep命令详解

grep 一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来 grep [option]  [mode]  [file] -c       #只输出匹配行的数量 -i       #搜索时忽略大小写 -h       #查询多文件时不显示文件名 -l       #只列出符合匹配的文件名,而不列出具体的匹配行 -n   #列出所有的匹配行,并显示行号 -s   #不显示不存在或无匹配文本的错误信息 -v   #显示不包含匹配文本的所有行 -w   #匹配整词 -x   #匹