練習:
1、顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式)
[root@localhost ~]# grep -i "^[sS]" /proc/meminfo SwapCached: 0 kB SwapTotal: 2098172 kB SwapFree: 2098172 kB Shmem: 6916 kB Slab: 179376 kB SReclaimable: 138252 kB SUnreclaim: 41124 kB
2、顯示/etc/passwd文件中不以/bin/bash結尾的行
grep -v "/bin/bash$" /etc/passwd #-v 顯示除了匹配到的行 #"/bin/bash$ 將$前面的字符串錨定到行尾
3、顯示用戶rpc默認的shell程序
[root@localhost ~]# grep "^rpc" /etc/passwd rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin #顯示過濾到的結果不正確。用戶rpc,rpc是一個單詞 #應該錨定單詞 [root@localhost ~]# grep "^\<rpc\>" /etc/passwd | cut -d: -f1,7 rpc:/sbin/nologin
4、找出/etc/passwd中的兩位或三位數
grep "[[:digit:]][[:digit:]][[:digit:]]\?" /etc/passwd 以下為部分內容 gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin 其中nfsnobody:x:65534:65534:##也被匹配到了 匹配到的原因是655、553、534.都是符合條件的。所以整個66534就都被匹配到了。 應該改為錨定單詞,將兩位數或三位數當成一個單詞來處理。 應該如下寫法 grep "\<[[:digit:]][[:digit:]][[:digit:]]\?\>" /etc/passwd
5、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行
[root@localhost ~]# grep "^[[:space:]]\+.*[^[:space:]]$" /etc/grub2.cfg
6、找出"netstat -tan"命令的結果中以'LISTEN'后跟0、1或多個空白字符結尾的行
[root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$" tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:631 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN
7、添加用戶bash、testbash、basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行
[root@localhost ~]# grep "^\([^:/]*\):.*/\1$" /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt bash:x:1001:1001::/home/bash:/bin/bash nologin:x:1004:1004::/home/nologin:/sbin/nologin
練習-用擴展正則表達式
1、顯示當前系統root、mage或wang用戶的UID和默認shell
[root@localhost ~]# egrep "^root|^wang|^mage" /etc/passwd root:x:0:0:root:/root:/bin/bash mageedu:x:1000:1000:mageedu:/home/mageedu:/bin/bash wang:x:1005:1005::/home/wang:/bin/bash
2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
[root@localhost ~]# grep "^[^([:space:]].*(" /etc/rc.d/init.d/functions [root@localhost ~]# egrep "^[^\([:space:]].*\(" /etc/rc.d/init.d/functions
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@localhost ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "[^/]*/?$" functions [root@localhost ~]#
4、使用egrep取出上面路徑的目錄名
[root@localhost ~]# echo "/etc/rc.d/init.d/functions" |egrep -o "^/.*/" /etc/rc.d/init.d/
5、統計以root身份登錄的每個遠程主機IP地址的登錄次數
[root@localhost ~]# last | egrep "[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*" | tr -s " " | sort -t" " -k3 -n |cut -d " " -f3 | uniq -c 5 192.168.226.1
6、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255
[0-9] [1-9]?[0-9] 2[0-4]?[0-9] 25[0-5]
7、顯示ifconfig命令結果中所有IPv4地址
常用ip地址是ABC類,地址范圍1.0.0.0-223.255.255.255 [root@localhost ~]# ifconfig |tr -s " " |egrep -o "^.*inet[[:space:]][[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*"| cut -d" " -f3 192.168.226.129 127.0.0.1
課后作業:
1、取本機ip地址
[root@localhost ~]# ifconfig |tr -s " " |egrep "broadcast" | cut -d" " -f3
192.168.226.129
2、取各分區利用率的數值
[root@localhost ~]# df | grep "^.*sda.*[[:digit:]]\+%"| tr -s " " |cut -d" " -f5,6 1% / 13% /usr 29% /boot
3、統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示
[root@localhost ~]# cat /etc/init.d/functions | tr -c "^[:alpha:]" "\n" | sort | uniq -c | sort -nr
5、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/" 取目錄名
[root@localhost ~]# vim lujing.sh #!/bin/bash #取路徑中除基名以外的路徑 echo "/etc/rc.d/functions" #顯示完整路徑 fullpath="/etc/rc.d/functions" #定義完整路徑變量 bcnum=`echo $fullpath |egrep -o "[^/]+/?$" | wc -c` #計算基名字數 fullnum=`echo $fullpath | wc -c` #計算完整路徑字數 pathcount="$[$fullnum-$bcnum]" #計算后的路徑字數也可以用bc計算 echo $fullpath|cut -c1-$pathcount #cut出路徑 [root@localhost ~]# bash lujing.sh /etc/rc.d/functions /etc/rc.d/ [root@localhost ~]#
原創文章,作者:yyw,如若轉載,請注明出處:http://www.www58058.com/30403