Linux · 2009-02-24

grep匹配与过滤 学习笔记-2

grep提取含有IP的行

root@debian:~# ifconfig eth0|grep “[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}”
inet addr:192.168.0.4 Bcast:192.168.0.255 Mask:255.255.255.0


root@debian:~# ifconfig eth0|grep -E “([0-9]{1,3}\.){3}[0-9]”
inet addr:192.168.0.4 Bcast:192.168.0.255 Mask:255.255.255.0



禁止显示错误信息

-s, –no-messages
Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, traditional
grep did not conform to POSIX.2, because traditional grep lacked a -q option and its -s option behaved like GNU
grep’s -q option. Shell scripts intended to be portable to traditional grep should avoid both -q and -s and
should redirect output to /dev/null instead.




将文件作为参数

root@debian:~/cu# cat kk
123
abc
www-data

root@debian:~/cu# egrep -f kk /etc/passwd
www-data:x:33:33:www-data:/var/www:/bin/sh

root@debian:~/cu# grep -E -f kk /etc/passwd
www-data:x:33:33:www-data:/var/www:/bin/sh



“|” “与” “或”
输出有123或abc的行

root@debian:~# echo -e 123″\n”abc”\n”AAA|grep -E “123|AAA”
123
AAA



关键字高亮 –color

root@debian:~# echo -e 123″\n”abc”\n”AAA|grep -E “123|AAA” –color
123
AAA