1、基本的正則表達(dá)式
- 顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
[root@centos7 app]#cat /proc/meminfo|grep "^[sS]"
SwapCached: 28 kB
SwapTotal: 2097148 kB
SwapFree: 2097060 kB
Shmem: 7796 kB
Slab: 108060 kB
SReclaimable: 50880 kB
SUnreclaim: 57180 kB
[root@centos7 app]#cat /proc/meminfo|grep -i "^s"
SwapCached: 28 kB
SwapTotal: 2097148 kB
SwapFree: 2097060 kB
Shmem: 7796 kB
Slab: 108060 kB
SReclaimable: 50880 kB
SUnreclaim: 57180 kB
- 顯示/etc/passwd文件中不以/bin/bash結(jié)尾的行
cat /etc/passwd |grep -v "/bin/bash$"
[root@centos7 app]#cat /etc/passwd |grep "^rpc\>"|cut -d: -f7
/sbin/nologin
[root@centos7 app]#cat /etc/passwd |grep -w "^rpc"|cut -d: -f7
/sbin/nologin
- 找出/etc/passwd中的兩位或三位數(shù)
grep -o "\<[0-9]\{2,3\}\>" /etc/passwd
- 顯示CentOS7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
cat /etc/grub2.cfg |grep "^[[:space:]]\+[^[:space:]]"
- 找出“netstat -tan”命令的結(jié)果中以‘LISTEN’后跟任意多個空白字符結(jié)尾的行
netstat -tan |grep "LISTEN[[:space:]]*$"
- 顯示CentOS7和centos6上所有系統(tǒng)用戶的用戶名和UID
cat /etc/passwd |cut -d: -f1,3|grep "\<[0-9]\{1,3\}\>"
cat /etc/passwd |cut -d: -f1,3|grep "\<\([0-9]\{1,2\}\|[1-4][0-9]\{2\}\)\>"
- 添加用戶bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名同shell名的行
grep "^\([[:alnum:]_]\+\>\).*\<\1$" /etc/passwd
- 利用df和grep,取出磁盤各分區(qū)利用率,并從大到小排序
[root@centos6 app]#df |grep "^/dev/sd"|tr -s " " "%"|cut -d% -f5|sort -nr
10
4
1
2、擴(kuò)展的正則表達(dá)式
- 顯示三個用戶root、mage、wang的UID和默認(rèn)shell
[root@centos6 app]#cat /etc/passwd |egrep "^(root|zhang|tom)\>"|cut -d: -f1,3,7
root:0:/bin/bash
zhang:500:/bin/bash
tom:504:/bin/bash
- 找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
egrep "^[[:alpha:]_]+\(\)" /etc/rc.d/init.d/functions
- 使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@centos6 app]#echo /etc/rc.d/init.d/functions |egrep -o "[^/]+/?$"
functions
[root@centos6 app]#echo /etc/rc.d/init.d/functions/ |egrep -o "[^/]+/?$"
functions/
[root@centos6 app]#echo /etc/rc.d/init.d/functions/ |egrep -o "^/.*/\<"
/etc/rc.d/init.d/
[root@centos6 app]#echo /etc/rc.d/init.d/functions |egrep -o "^/.*/\<"
/etc/rc.d/init.d/
- 統(tǒng)計(jì)last命令中以root登錄的每個主機(jī)IP地址登錄次數(shù)
[root@centos6 app]#last |egrep "^root\>"|egrep -o "(\<[0-9]{1,3}\.){3}\<[0-9]{1,3}\>"|sort|uniq -c
59 192.168.25.1
- 利用擴(kuò)展正則表達(dá)式分別表示0-9、10-99、100-199、200-249、250-255
echo {0..255}|egrep -o "\<\[0-9]>"
echo {0..255}|egrep -o "\<[1-9][0-9]\>"
echo {0..255}|egrep -o "\<1[0-9]{2}\>"
echo {0..255}|egrep -o "\<2[0-4][0-9]\>"
echo {0..255}|egrep -o "\<25[0-5]\>"
- 顯示ifconfig命令結(jié)果中所有IPv4地址
ifconfig |egrep -o "\<(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
- 將此字符串:welcome to magedu linux 中的每個字符去重并排序,重復(fù)次數(shù)多的排到前面
echo "welcome to magedu linux" |egrep -o [[:alpha:]] |sort|uniq -c
- 將/misc/cd/Packages 文件中以.rpm結(jié)尾的文件,cpu架構(gòu)取出來,并統(tǒng)計(jì)每種架構(gòu)出現(xiàn)的次數(shù)
[root@centos6 Packages]#ls *.rpm |egrep -o "[^.]*\.rpm$"|cut -d "." -f1|sort|uniq -c
4 i686
925 noarch
2311 x86_64
[root@centos6 Packages]#ls *.rpm |rev|cut -d "." -f2|rev|sort|uniq -c
4 i686
925 noarch
2311 x86_64