1钧惧、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
grep "^[sS]" /proc/meminfo
grep "^(s|S)" /proc/meminfo
egrep "^(s|S)" /proc/meminfo
grep -e ^s -e ^S /proc/meminfo
2进鸠、顯示/etc/passwd文件中不以/bin/bash結尾的行
grep -v "/bin/bash$" /etc/passwd
3、顯示用戶rpc默認的shell程序
getent passwd rpc|cut -d: -f1,7
grep "^rpc>" /etc/passwd |cut -d: -f1,7
4嘀粱、找出/etc/passwd中的兩位或三位數
grep -o "<[0-9]{2,3}>" /etc/passwd
5、顯示CentOS7的/etc/grub2.cfg文件中约谈,至少以一個空白字符開頭的且后面存非空白字符的行
grep "[[:space:]]{1,}[[:space:]]{1,}" /etc/grub2.cfg
6指巡、找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多個空白字符結尾的行
netstat -tan|egrep -o "<LISTEN>[[:space:]]$"
7、顯示CentOS7上所有系統(tǒng)用戶的用戶名和UID
cat /etc/passwd|cut -d: -f1,3|egrep "<[0-9]{1,3}>"
8火鼻、添加用戶bash室囊、testbash、basher魁索、sh融撞、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名同shell名的行
cat /etc/passwd|egrep "(^.)>./\1$"
egrep "(^.)>.*<\1$" /etc/passwd
9、利用df和grep粗蔚,取出磁盤各分區(qū)利用率尝偎,并從大到小排序?
df |gerp -o "^/dev/sd"|egrep "<[0-9]+%>"|tr -d %story -nr
df |grep "^/dev/sd"|tr -s " " "%"|cut -d% -f5|sort -nr
10、顯示三個用戶root鹏控、mage致扯、wang的UID和默認shell
grep "^(root>|mage|wang)>" /etc/passwd|cut -d: -f1,3,7
egrep "^(root|mage|wang)>" /etc/passwd|cut -d: -f1,3,7
11趁窃、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
egrep -o "^.\b()" /etc/rc.d/init.d/functions
egrep -o "^.>()" /etc/rc.d/init.d/functions
egrep -o '^([[:alnum:]]|)+()' /etc/rc.d/init.d/functions
egrep -o '^[[:alnum:]]+()' /etc/rc.d/init.d/functions
egrep -o '^[a-zA-Z0-9_]+()' /etc/rc.d/init.d/functions
12、使用egrep取出/etc/rc.d/init.d/functions中其基名
echo /etc/rc.d/init.d/functions|cut -d"/" -f5
echo /etc/rc.d/init.d/functions|egrep -o "[^/]+$" 文件
echo /etc/rc.d/init.d/functions |grep -Eo "[[:alnum:]]+$" 文件
echo /etc/rc.d/init.d/functions|egrep -o "[^/]+/?$"
basename /etc/rc.d/init.d/functions
13急前、使用egrep取出上面路徑的目錄名
echo /etc/rc.d/init.d/functions|egrep -o "/./."|grep -o ""/./"
dirname /etc/rc.d/init.d/functions
14醒陆、統(tǒng)計last命令中以root登錄的每個主機IP地址登錄次數
last |egrep -o "^root>"|egrep -o"([0-9]|[0-9]{2}|[0-9]{3})(.([0-9]|[0-9]{2}|[0-9]{3})){3}"|sort|uniq -c
15、利用擴展正則表達式分別表示0-9裆针、10-99刨摩、100-199、200-249世吨、250-255
[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}(([0-9]|[1-9][0-9]|[1[0-9][{2}|2[0-4][0-9]|25[0-5])
16澡刹、顯示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])>"
17、將此字符串:welcome to magedu linux 中的每個字符去重并排序耘婚,重復次數多的排到前面
echo "welcome to magedu linux" |tr -d " " |egrep -o [[:alnum:]] |sort|uniq -c|sort -nr
echo "welcome to magedu linux" |grep -o '.'|sort |uniq -c|sort -nr