grep正則表達式課堂練習
1、找出ifconfig命令結果中本機的所有IPv4地址
# ifconfig | head -2 | cut -dt -f2 | cut -dn -f1 | tail -1
2、 查出分區空間使用率的最大百分比值
# df | cut -c56-58 | sort -n | tail -1
3、 查出用戶UID最大值的用戶名、UID及shell類型
# sort -nrt: -k3 /etc/passwd |head -n1 |cut -d: -f1,3,7
4、 查出/tmp的權限,以數字方式顯示
# stat /tmp | head -4 | tail -1 | cut -d/ -f1 | cut -d\( -f2
5、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序
# netstat -tn | cut -d: -f2 |tail -1 |tr -s " " ":" |sort |uniq -c
6、 顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式)
# grep -i "^[sS]" /proc/meminfo
# grep -e "^s" -e "^S" /proc/meminfo
7、顯示/etc/passwd文件中不以/bin/bash結尾的行
# grep -v "/bin/bash$" /etc/passw
8、顯示用戶rpc默認的shell程序
#getent passwd | grep -w "^rpc\b"
9、找出/etc/passwd中的兩位或三位數
# grep "[[:digit:]]\{2,3\}" /etc/passwd
10、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
# grep "^[[:space:]]\+[^[:space:]].*" /etc/grub2.cfg
11、找出"netstat -tan"命令的結果中以'LISTEN'后跟0、1或多個空白字符結尾的行
# netstat -tan | grep "\bLISTEN[[:space:]]*$"
12、添加用戶bash、testbash、basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行
#useradd bash
#useradd testbash
#useradd basher
#useradd -s /sbin/nologin nologin
# getent passwd |grep "^\(\b.*\b\).*/\1$"
原創文章,作者:Aaron_wang,如若轉載,請注明出處:http://www.www58058.com/29394