一.文本处理练习

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