一.文本處理練習正則表達式
一、找出ifconfig命令結果中本機的全部IPv4地址shell
[root@centos7 zhang]# ifconfig |tr -s " " |head -2 |tail -1| cut -d " " -f3
二、查出分區空間使用率的最大百分比值centos
[root@centos7 zhang]# df |cut -c46-48 |sort -nr |head -1
三、查出用戶UID最大值的用戶名、UID及shell類型bash
[root@centos7 zhang]# cat /etc/passwd |sort -t: -k3 -n |tail -1 |cut -d: -f1,3,7
四、查出/tmp的權限,以數字方式顯示ide
[root@centos7 zhang]# stat /tmp |head -4|tail -1 |cut -d "/" -f1 |cut -d "1" -f2
五、統計當前鏈接本機的每一個遠程主機IP的鏈接數,並按從大到小排序centos7
[root@centos7 zhang]# netstat -nt |cut -d : -f2 |tr -s " " ":" |cut -d: -f2 |sort -r |uniq -c
二.grep練習spa
一、顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式)排序
[root@centos7 zhang]# grep -i "^s" /proc/meminfo 或[root@centos7 zhang]# grep "^[Ss]" /proc/meminfo 或[root@centos7 zhang]# egrep "^(S|s)" /proc/meminfo
二、顯示/etc/passwd文件中不以/bin/bash結尾的行rpc
[root@centos7 zhang]# grep "/bin/bash$" /etc/passwd
三、顯示用戶rpc默認的shell程序it
[root@centos7 zhang]# grep "^rpc\>" /etc/passwd |cut -d: -f7
四、找出/etc/passwd中的兩位或三位數
[root@centos7 zhang]# grep "\<[0-9]\{2,3\}\>" /etc/passwd
五、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且後面存非空白字符的行
[root@centos7 zhang]# grep -n "^[[:space:]]\+[^[:space:]]*" /etc/grub2.cfg
六、找出"netstat -tan"命令的結果中以'LISTEN'後跟0、1或多個空白字符結尾的行
[root@centos7 zhang]# netstat -tan |grep -n "\<LISTEN[[:space:]]*$"
七、添加用戶bash、testbash、basher以及nologin(其shell爲/sbin/nologin),然後找出/etc/passwd文件中用戶名和shell相同的行
[root@localhost testdir]# grep "^\([[:alnum:]]\+\):.*\1$" /etc/passwd 或[root@localhost testdir]# grep "^\(\<.*\>\).*\1$" /etc/passwd
三.egrep練習
一、顯示當前系統root、mage或wang用戶的UID和默認shell
[root@centos7 zhang]# egrep "(root|zhang)" /etc/passwd |cut -d: -f3,7
二、找出/etc/rc.d/init.d/functions文件中行首爲某單詞(包括下劃線)後面跟一個小括號的行
[root@centos7 zhang]# egrep "^\<([[:alpha:]]|_)+\>\(\).*" /etc/rc.d/init.d/functions
三、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@centos7 zhang]# echo "/etc/rc.d/init.d/functions" |egrep -o "[[:alpha:]]+$" 或 [root@centos7 zhang]# echo "/etc/rc.d/init.d/functions" |egrep -o "[^/]+$"
四、使用egrep取出上面路徑的目錄名
[root@centos7 zhang]# echo "/etc/rc.d/init.d/fun.ctions" |egrep -o "^.*/"
五、統計以zhang身份登陸的每一個遠程主機IP地址的登陸次數
[root@centos7 zhang]# w|egrep "^zhang" |sort -nr |tr -s " " ":"|cut -d: -f3 |uniq -c
六、利用擴展正則表達式分別表示0-九、10-9九、100-19九、200-24九、250-255
"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]"
七、顯示ifconfig命令結果中全部IPv4地址
[root@centos7 Packages]# ifconfig |egrep "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
三.做業
一、用正則表達式表示IP地址
[root@centos7 Packages]# ifconfig |egrep "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
二、用正則表達式表示手機號11 13 17 15 18
grep "\<1[13578][0-9]{9}\>"
三、用正則表達式表示×××號18
egrep "\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|([0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>" filename
四、用正則表達式表示郵箱
[root@centos7 testdir]# egrep "\b[[:alnum:]]+(-|_)*[[:alnum:]]\b@([[:alnum:]]+\.)+[[:alnum:]]+" mail
四.sed練習
一、刪除/etc/grub2.conf文件中全部以空白開頭的行行首的空白字符
[root@centos7 ~]# sed -r 's@^[[:space:]]+@@' /etc/grub2.cfg
二、刪除/etc/fstab文件中全部以#開頭,後面至少跟一個空白字符的行的行首的#和空白字符
[root@centos7 ~]# sed 's/^#[[:space:]]\+//' /etc/fstab
三、在/etc/passwd每一行行首增長#號
[root@centos7 ~]# sed 's/^/#/' /etc/passwd
四、在/etc/fstab文件中不以#開頭的行的行首增長#號
[root@centos7 ~]# sed 's/^[^#]/#/' /etc/fstab 或[root@centos7 ~]# sed -r 's/^([^#].*)/#\1/' /etc/fstab
五、處理/etc/fstab路徑,使用sed命令取出其目錄名和基名
echo "/etc/fst/sd" | sed -r 's@(.*)/([^/]+/?)@\2@' 取基名 echo "/etc/fst/sd" | sed -r 's@(.*/)([^/]+/?)$@\1@'取目錄名 或[root@centos7 ~]# echo "/etc/fstab/asdd" |sed -r 's@[^/]+\/?$@@' 取目錄名 [root@centos7 ~]# echo "/etc/fstab/asdd" |sed -r 's@^.*/@@' 取基名
六、利用sed 取出ifconfig命令中本機的IPv4地址
[root@centos7 ~]# ifconfig |sed -n '2p' |sed -r 's@^[[:space:]]+inet@@' |sed -r 's@net.*$@@'
七、統計centos安裝光盤中Package目錄下的全部rpm文件的以.分隔倒數第二個字段的重複次數
[root@centos7 Packages]# ls *.rpm |sed -r 's@.*\.(.*)\.rpm@\1@' |sort |uniq -c