gaohan 发表于 2018-5-23 06:45:27

8 Linux之grep


[*]  额,这里不能抱怨由于网络原因,几次写博客都被牛逼哄哄的网速给回了,dan疼啊!

[*]  文本搜索工具:grep,egrep,fgrep

  1(1)grep "^[[:alpha:]]*" /proc/meminfo
  (2)
grep "^\+" /proc/meminfo  2 grep -v ".*\/sbin\/nologin.*" /etc/passwd | cut -d: -f1
  grep -v ".*\/sbin\/nologin$" /etc/passwd | cut -d: -f1
  3 grep ".*\/bin\/bash.*" /etc/passwd | cut -d: -f1
  grep ".*\/bin\/bash$" /etc/passwd | cut -d: -f1
  4 grep "\<[[:digit:]][[:digit:]]\>" /etc/passwd --color=auto
grep "\<[[:digit:]]\{2\}\>" /etc/passwd --color=auto  5
grep "^[[:space:]]\{1,\}.*" /boot/grub/grub.conf
   grep "^[[:space:]]\+"  6 显示当前系统上root、fedora、或user1用户的默认shell
     
egrep --color "^root|^fedora|^user1" /etc/passwd | cut -d: -f1,7
   egrep --color "^(root|fedora|user1)\>" /etc/passwd | cut -d: -f1,7  

  7 找出/etc/rc.d/init.d/functions 文件中某单词后面跟一组小括号的行,如:hello()
     
egrep --color=auto "*\<[[:alnum:]]+\>\(\)" /etc/rc.d/init.d/functions |cut -d' ' -f1  -o选项
  8 echo "/etc/sysconfig" | grep
     # basename /etc/sysconfig/
            sysconfig
  echo "etc/sysconfit/net-scripts"| egrep --color=auto "*[^/]+$"
     # basename /etc/sysconfig
            sysconfig
  扩展:取出其路径名
  9 找出ifconfig命令结果中的1-255之间的数字
ifconfig | egrep "\<(||1|2)\>" --color=auto  10 挑战:写个模式,能匹配合理的IP地址

     
  11 挑战:写个模式,能匹配出所有的邮件地址
     由于具体邮件地址规则不详,故下面是自己写的,感觉还不错
egrep --color=auto "^[[:alnum:]]+(_?[[:alnum:]]+){0,}@([[:alnum:]]+\.){0,}[[:alnum:]]+$" matchmail.regexp  

  

  
页: [1]
查看完整版本: 8 Linux之grep