1、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
grep "s|S" /proc/meminfo
grep "^(s|S)" /proc/meminfo
cat /proc/meminfo | grep -i "^s"
cat /proc/meminfo | grep "^[sS]"
grep -e ^s -e ^S /proc/meminfo
grep -i "^s" /proc/meminfo
grep "^[Ss]" /proc/meminfo
2掷酗、顯示/etc/passwd文件中不以/bin/bash結(jié)尾的行
grep -v "/bin/bash" /etc/passwd grep -v ".*\" /etc/passwd
3扛施、顯示用戶rpc默認(rèn)的shell程序
getent passwd rpc |cut -d: -f7
grep "^rpc>" /etc/passwd|cut -d: -f7
grep "^rpc\b" /etc/passwd|cut -d: -f7
grep -w "^rpc" /etc/passwd|cut -d: -f7
grep "^<rpc>" /etc/passwd|grep -o "/[[:alpha:]]+/[[:alpha:]]+$"
4、找出/etc/passwd中的兩位或三位數(shù)
grep -o "<[0-9]{2,3}>" /etc/passwd
grep -o "\b[0-9]{2,3}\b" /etc/passwd
grep -o "<[[:digit:]]{2,3}>" /etc/passwd
5涛菠、顯示CentOS7的/etc/grub2.cfg文件中莉御,至少以一個空白字符開頭的且后面存非空白字符的行
grep "[[:space:]]+[[:space:]]" /etc/grub2.cfg
grep "^[[:space:]]+" /etc/grub2.cfg |grep "[]" grep "^[[:space:]]\+" /etc/grub2.cfg|grep -v "^"
6、找出“netstat -tan”命令的結(jié)果中以‘LISTEN’后跟任意多個空白字符結(jié)尾的行
netstat -tan|grep "LISTEN[[:space:]]*$"
7俗冻、顯示CentOS7上所有系統(tǒng)用戶的用戶名和UID
grep "<[0-9]{1,3}" /etc/passwd|cut -d: -f1,3 cut -d: -f1,3 /etc/passwd|grep -v "[0-9]\{4,\}" (因為考慮到GID有可能是4位數(shù)礁叔,UID是3位數(shù)以內(nèi),所以先cut) cut -d: -f1,3 < /etc/passwd |egrep "\b[0-9]{1,3}"
不完整:grep -o "^[[:alpha:][:punct:]]+:x:<[[:digit:]]{1,3}>" /etc/passwd
8迄薄、添加用戶bash琅关、testbash、basher讥蔽、sh涣易、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名同shell名的行
grep "^(<[[:alpha:]]+>)./\1" /etc/passwd grep "\(^[[:alpha:]]\+\>\).*/\1" /etc/passwd
grep "(^[[:alnum:]]+>)./\1" /etc/passwd grep "^\(.*\):.*\<\1" /etc/passwd;
grep "^(.)>.<\1" /etc/passwd grep "^\(.*\):.*\b\1" /etc/passwd
9、利用df和grep冶伞,取出磁盤各分區(qū)利用率新症,并從大到小排序
df|grep "/dev/sd"|grep -o "<[0-9]{1,2}>"|sort -nr***不嚴(yán)謹(jǐn),會有bug--<[0-9]{1,2}>前面的信息可能會匹配到
df|grep "/dev/sd"|grep -o "[0-9]{1,3}%"|grep -o "[0-9]{1,3}"|sort -rn
df|grep "/dev/sd"|tr -s " " "%"|cut -d% -f5|sort -nr
df|grep "/dev/sd"|grep -o "[0-9]{1,2}%"|cut -d% -f1|sort -nr
1响禽、顯示三個用戶root徒爹、mage、wang的UID和默認(rèn)shell
grep "^<(root|mage|wang)>" /etc/passwd|cut -d: -f1,7
cut -d: -f1,7 /etc/passwd|grep -e "<root>" -e "<mage>" -e "<wang>"
2金抡、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
cat /etc/rc.d/init.d/functions |grep -o "^<([[:alpha:]_])+>()"
3瀑焦、使用egrep取出/etc/rc.d/init.d/functions中其基名
basename /etc/rc.d/init.d/functions
echo /etc/rc.d/init.d/functions |egrep -o "[^/]+/?" echo /etc/rc.d/init.d/functions|egrep -o "/[[:alpha:]]+"|egrep -o "[[:alpha:]]+"
4、使用egrep取出上面路徑的目錄名
dirname /etc/rc.d/init.d/functions
echo /etc/rc.d/init.d/functions |egrep -o "^[/].*/"
5梗肝、統(tǒng)計last命令中以root登錄的每個主機IP地址登錄次數(shù)
last|egrep "root"|egrep --color=auto -o "([0-9]{1,3}.){3}[0-9]{1,3}"|sort|uniq -c
6榛瓮、利用擴展正則表達(dá)式分別表示0-9、10-99巫击、100-199禀晓、200-249精续、250-255
0-9:echo {0..9}|egrep "[0-9]"
10-99:echo {10..99}|egrep "[1-9][0-9]"
100-199:echo {100..199}|egrep "1[0-9]{2}"
200-249:echo {200..249}|egrep "2[0-4][0-9]"
250-255:echo {250..255}|egrep "25[0-5]"
7、顯示ifconfig命令結(jié)果中所有IPv4地址
基礎(chǔ)版:ifconfig ens33|grep -o "([0-9]{1,3}.){3}[0-9]{1,3}"
ifconfig ens33|grep -o "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
ifconfig ens33|grep -Eo "([0-9]{1,3}.){3}[0-9]{1,3}"
ifconfig ens33|egrep -o "([0-9]{1,3}.){3}[0-9]{1,3}"
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 中的每個字符去重并排序重付,重復(fù)次數(shù)多的排到前面
echo welcome to magedu linux|grep -o "."|sort|uniq -c|sort -nr