文本處理練習

一.文本處理練習

1、找出ifconfig命令結果中本機的所有IPv4地址

[root@centos7 zhang]# ifconfig |tr -s " " |head -2 |tail -1| cut -d " " -f3

2、查出分區空間使用率的最大百分比值

[root@centos7 zhang]# df |cut -c46-48 |sort -nr |head -1

3、查出用戶UID最大值的用戶名、UID及shell類型

[root@centos7 zhang]# cat /etc/passwd |sort -t: -k3 -n |tail -1 |cut -d: -f1,3,7

4、查出/tmp的權限,以數字方式顯示

[root@centos7 zhang]# stat /tmp |head -4|tail -1 |cut -d "/" -f1 |cut -d "1" -f2

5、統計當前連接本機的每個遠程主機IP的連接數,並按從大到小排序

[root@centos7 zhang]# netstat -nt |cut -d : -f2 |tr -s " " ":" |cut -d: -f2 |sort -r |uniq -c


二.grep練習

1、顯示/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

2、顯示/etc/passwd文件中不以/bin/bash結尾的行

[root@centos7 zhang]# grep  "/bin/bash$" /etc/passwd

3、顯示用戶rpc默認的shell程序

[root@centos7 zhang]# grep "^rpc\>" /etc/passwd |cut -d: -f7

4、找出/etc/passwd中的兩位或三位數

[root@centos7 zhang]# grep "\<[0-9]\{2,3\}\>" /etc/passwd

5、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且後面存非空白字符的行

[root@centos7 zhang]# grep -n "^[[:space:]]\+[^[:space:]]*" /etc/grub2.cfg

6、找出"netstat -tan"命令的結果中以'LISTEN'後跟0、1或多個空白字符結尾的行

[root@centos7 zhang]# netstat -tan |grep -n "\<LISTEN[[:space:]]*$"

7、添加用戶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練習


1、顯示當前系統root、mage或wang用戶的UID和默認shell

[root@centos7 zhang]# egrep "(root|zhang)" /etc/passwd |cut -d: -f3,7

2、找出/etc/rc.d/init.d/functions文件中行首爲某單詞(包括下劃線)後面跟一個小括號的行

[root@centos7 zhang]# egrep "^\<([[:alpha:]]|_)+\>\(\).*" /etc/rc.d/init.d/functions

3、使用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 "[^/]+$"

4、使用egrep取出上面路徑的目錄名

[root@centos7 zhang]# echo  "/etc/rc.d/init.d/fun.ctions" |egrep -o "^.*/"

5、統計以zhang身份登錄的每個遠程主機IP地址的登錄次數

[root@centos7 zhang]# w|egrep "^zhang" |sort -nr |tr -s " " ":"|cut -d: -f3 |uniq -c

6、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255

"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]"

7、顯示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])\>"



三.作業

1、用正則表達式表示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])\>"

2、用正則表達式表示手機號11 13 17 15  18 

grep "\<1[13578][0-9]{9}\>"

 

3、用正則表達式表示身份證號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

4、用正則表達式表示郵箱

[root@centos7 testdir]# egrep "\b[[:alnum:]]+(-|_)*[[:alnum:]]\b@([[:alnum:]]+\.)+[[:alnum:]]+" mail


四.sed練習

1、刪除/etc/grub2.conf文件中所有以空白開頭的行行首的空白字符

[root@centos7 ~]# sed -r 's@^[[:space:]]+@@' /etc/grub2.cfg

2、刪除/etc/fstab文件中所有以#開頭,後面至少跟一個空白字符的行的行首的#和空白字符

[root@centos7 ~]# sed 's/^#[[:space:]]\+//' /etc/fstab

3、在/etc/passwd每一行行首增加#號

[root@centos7 ~]# sed 's/^/#/' /etc/passwd

4、在/etc/fstab文件中不以#開頭的行的行首增加#號

[root@centos7 ~]# sed 's/^[^#]/#/' /etc/fstab
或[root@centos7 ~]# sed -r 's/^([^#].*)/#\1/' /etc/fstab

5、處理/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@^.*/@@'     取基名

6、利用sed 取出ifconfig命令中本機的IPv4地址

[root@centos7 ~]# ifconfig |sed -n '2p' |sed -r 's@^[[:space:]]+inet@@' |sed -r 's@net.*$@@'

7、統計centos安裝光盤中Package目錄下的所有rpm文件的以.分隔倒數第二個字段的重複次數

[root@centos7 Packages]# ls *.rpm |sed -r 's@.*\.(.*)\.rpm@\1@' |sort |uniq -c

















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章