課堂練習題
1、找出ifconfig命令結果中本機的所有IPv4地址
# ifconfig | grep -oE "([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}"
2、查出分區空間使用率的最大百分比值
# df |grep "/dev/sd" | tr -s " " ":" |cut -d: -f1,5 |sort
3、查出用戶UID最大值的用戶名、UID及shell類型
# cat /etc/passwd | sort -n -t: -k3 | tail -1 | cut -d: -f1,3,7
4、查出/tmp的權限,以數字方式顯示
# stat /tmp |grep 'Access' | head -1 | tr " " ":" | cut -d: -f3 |tr -cd "[[:digit:]]"
5、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序
[root@centos7 ~]# netstat -nt | grep "tcp"|tr -s " " ":"|cut -d: -f6 |sort |uniq -c
6、顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式)
(1)grep "^[sS]" /proc/meminfo
(2)grep -i "^s" /proc/meminfo
7、顯示/etc/passwd文件中不以/bin/bash結尾的行
[root@centos7 ~]# grep -v "/bin/bash$" /etc/passwd
8、顯示用戶rpc默認的shell程序
grep "^rpc\>" /etc/passwd | cut -d: -f1,7
9、找出/etc/passwd中的兩位或三位數
[root@centos7 ~]# grep -E "\b([1-9][0-9]|[1-9][0-9]{2})\b" /etc/passwd
10、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
[root@centos7 ~]# grep "^\s[^\s].*" /etc/grub2.cfg
11、找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多個空白字符結尾的行
[root@centos7 ~]# netstat -tan | grep "LISTEN\s*$"
12、添加用戶bash、testbash、basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行
[root@centos7 ~]# grep -E "(^[[:alnum:]]+)\b.*/\1{1}$" /etc/passwd
13、顯示三個用戶root、mage、wang的UID和默認shell
[root@centos7 ~]# grep -E "^(root|mage|wang)\>" /etc/passwd | cut -d: -f1,3,7
14、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
[root@centos7 ~]# egrep "\w+\(\).*" /etc/rc.d/init.d/functions
15、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" | egrep "\w*\.?\w*$"
16、統計以root身份登錄的每個遠程主機IP地址的登錄次數
[root@centos7 ~]# last | grep "root" | egrep -o "([0-9]|[0-9]{2}|[0-9]{3})(\.([0-9]|[0-9]{2}|[0-9]{3})){3}"|sort|uniq -c
17、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255
[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]
18、顯示ifconfig命令結果中所有IPv4地址
[root@centos7 ~]# ifconfig | egrep -o "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}"
作業題
1、取本機ip地址
[root@centos7 ~]# ifconfig|head -2|tail -1|grep -oE "([0-9]|[1-9][0-9]|[1-9][0-9]{2})(\.([0-9]|[1-9][0-9]|[1-9][0-9]{2})){3}"|head -1
2、取各分區利用率的數值
[root@centos7 ~]# df -h | grep "/dev/sd" | tr -s " " ":" | cut -d: -f1,5 | sort
3、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示
[root@centos7 ~]# grep -o "\b[[:alnum:]]*\b" /etc/init.d/functions | sort | uniq -c | sort -n
4、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/" 取目錄名
[root@centos7 ~]# echo "/etc/rc.d/init.d/functions" | egrep -o "/.*/"
5、正則表達式表示身份證號
egrep "\b[1-9]{2}[0-9]{15}[0-9xX]\b"
6、正則表達式表示手機號
egrep "\b1[3-8][0-9]{9}\b"
7、正則表達式表示郵箱
[root@centos7 ~]# egrep -i "\w*@[[:alnum:]]*\.[[:alpha:]]{1,3}\.?[[:alpha:]]{,2}"
8、正則表達式表示QQ號
[root@centos7 ~]# egrep "\b[1-9][0-9]{4,9}\b"
原創文章,作者:~微風~,如若轉載,請注明出處:http://www.www58058.com/30832