1 、显示三个用户root、mage、song的UID和默认shell

  1. egrep "^(root|mage|song)" /etc/passwd | cut -d: -f1,3,7

2 、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

  1. egrep "^([[:alnum:]_])+\(\).*" /etc/rc.d/init.d/functions

3 、使用egrep取出/etc/rc.d/init.d/functions中其基名

  1. echo "/etc/rc.d/init.d/functions" | egrep -o "[^/]+/?$"

4 、使用egrep取出上面路径的目录名

  1. echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/"

5 、统计last命令中以root登录的每个主机IP地址登录次数

  1. last | egrep root | egrep "\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>" | tr -s " " ":" | sort -t: -k3 | cut -d: -f3|uniq -c

6 、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

  1. [0-9]
  2. [1-9][0-9]
  3. 1[0-9]\{2\}
  4. 2[0-4][0-9]
  5. 25[0-5]

7 、显示ifconfig命令结果中所有IPv4地址

ifconfig |egrep -o  "\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"

8 、将此字符串:welcome to magedu linux 中的每个字符

去重并排序,重复次数多的排到前面

echo "welcome to magedu linux" |tr -d " " | grep -o "."| sort | uniq -c | sort -nr