4、经典场景
除非要精确区分大小写,否则请加上-i来忽略大小写
(1)结合find命令和管道[root@localhost ~]#find . -name ".mp3" | grep -i jay | grep -vi "remix"分析: 1)使用find -name 来列出所有mp3文件,重定向给grep
[root@localhost ~]# 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 [root@localhost ~]# 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
root@localhost ~]# grep -c "*baidu.com*" filename 例子 [root@localhost ~]# 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 [root@localhost ~]# grep -cn ".*baidu.com.*" file.txt 3
[root@localhost ~]# grep -nr HELLO_HWC_CSND_BLOG* .
例子:
[root@localhost ~]# 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
例子:
[root@localhost ~]# grep -lr baidu . ./file.txt ./test/test.txt
[root@localhost ~]# 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
[root@localhost ~]# 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
例子
[root@localhost ~]# ls anaconda-ks.cfg Desktop file.txt find.result install.log install.log.syslog test [root@localhost ~]# 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目录
[root@localhost ~]# 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
[root@localhost ~]# 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 [root@localhost ~]# grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt 192.168.0.1 162.12.0.123
(9)查找邮箱
[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt
例子
[root@localhost ~]# 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 [root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt [email protected] [email protected]