1.显示三个用户root、JK的UID和默认shell
grep -E "^(root|JK)" /etc/passwd | cut -d: -f3,7
2.找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
grep -E "^\w+\(\)" /etc/rc.d/init.d/functions
3.使用grep取出/etc/rc.d/init.d/functions中其基名
echo "/etc/rc.d/init.d/functions/" | grep -Eo "[^/]+\/?$"
4使用grep取出/etc/rc.d/init.d/functions中的路径名
echo "/etc/rc.d/init.d/functions" | grep -Eo "^/.*/"
5.统计last命令中以root登录的每个主机IP地址登录次数
last | grep -E "^root" | grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}" | sort | uniq -c | sort -nr
6.显示ifconfig命令结果中所有IPv4地址
ifconfig | grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}"
7.将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面
echo "welcome to magedu linux" | grep -Eo "[[:alpha:]]" | sort | uniq -c | sort -nr
8.利用df和grep,取出磁盘各分区利用率,并从大到小排序
df | grep -Eo "[0-9]{1,3}%" | cut -d% -f1 | sort -nr
9.找出/etc/passwd用户 名和shell同名的行
grep -E "^([[:alnum:]]+\>).*\1$" /etc/passwd
10.显示CentOS7上所有UID小于1000以内的用户名和UID
grep -E ":[0-9]{1,3}:[0-9]+" /etc/passwd | cut -d: -f1,3
11.找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行
netstat -tan | grep -Ei "listen[[:space:]]*" | cat -A
12.找出/etc/passwd中的两位或三位数
grep -Eo "\<[0-9]{2,3}\>" /etc/passwd
13.显示用户rpc默认的shell程序
grep -E "^JK" /etc/passwd | cut -d: -f7
14.显示/etc/passwd文件中不以/bin/bash结尾的行
grep -Ev "/bin/bash$" /etc/passwd
15.显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)
cat /proc/meminfo | grep -i "^s"
cat /proc/meminfo | grep "^[sS]"