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

grep提取含有IP的行

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



禁止显示错误信息

  1. -s, --no-messages
  2. Suppress error messages about nonexistent or unreadable filesPortability noteunlike  GNU  greptraditional
  3. grep  did  not  conform to POSIX.2, because traditional grep lacked a -q option and its -s option behaved like GNU
  4. greps -q optionShell scripts intended to be portable to traditional grep should  avoid  both  -q  and  -s  and
  5. should redirect output to /dev/null instead.




将文件作为参数

  1. root@debian:~/cu# cat kk
  2. 123
  3. abc
  4. www-data
  5.  
  6. root@debian:~/cu# egrep -f kk /etc/passwd
  7. www-data:x:33:33:www-data:/var/www:/bin/sh
  8.  
  9. root@debian:~/cu# grep -E -f kk /etc/passwd
  10. www-data:x:33:33:www-data:/var/www:/bin/sh



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

  1. root@debian:~# echo -e 123"\n"abc"\n"AAA|grep -E "123|AAA"
  2. 123
  3. AAA



关键字高亮 –color

  1. root@debian:~# echo -e 123"\n"abc"\n"AAA|grep -E "123|AAA" --color
  2. 123
  3. AAA


6 Comments

  1. 囧,我也不懂,有钱途~~