文本处理工具(cut,sort,uniq)练习

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

[root@localhost ~]# ifconfig | tr -cs '[:digit:].' '\n'| sort -t. -k3 |tail -5

1.png

2、查出分区空间使用率的最大百分比值

[root@localhost ~]#  df -h | tr -s ' ' ':'|cut -d: -f5|tr -d '%'|sort -n|tail -1

2.png

3、查出用户UID最大值的用户名、UID及shell类型

[root@localhost ~]# cut -d: -f1,3,7 /etc/passwd | sort -t: -k2 -n | tail -1

3.png

4、查出/tmp的权限,以数字方式显示

[root@localhost ~]# stat  /tmp | head -4 | tail -1 | tr -s '(/' '::' | cut -d: -f3

4.png

5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@localhost ~]# netstat -nt |tr -s ' ' ':' |cut -d: -f6 | tr -d '[:alpha:]' | sort -r | uniq -c | sort -r

5.png


grep正则表达式练习

1、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)

[root@localhost ~]# grep  -i  '^s' /proc/meminfo

[root@localhost ~]# grep  '^[Ss]' /proc/meminfo

G1.png


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

[root@localhost ~]# grep -v '/bin/bash$' /etc/passwd

G2.png

3、显示用户rpc默认的shell程序

[root@localhost ~]# grep '^rpc\>' /etc/passwd | cut -d: -f7

G3.png

4、找出/etc/passwd中的两位或三位数

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

G4.png

5、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行

[root@localhost ~]# grep "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg

G5.png

6、找出"netstat -tan"命令的结果中以'LISTEN'后跟任意多个空白字符结尾的行

[root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$"

G6.png

7、添加用户bash、testbash、basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行

[root@localhost ~]# grep "^\([^:]\+\>\).*/\1$" /etc/passwd

G7.png


egrep扩张正则表达式


1、显示三个用户root、mage、wang的UID和默认shell

[root@localhost ~]# egrep "^(root|mage|wang)\>" /etc/passwd | cut -d -f1,3,7

EG1.png

2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@localhost ~]# egrep "[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions

EG2.png

3、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@localhost ~]# echo "/etc/rc.d/init.d/functions" | egrep -o "[^/]+/?$"

EG3.png

4、使用egrep取出上面路径的目录名

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

EG4.png

5、统计以root身份登录的每个远程主机IP地址的登录次数

[root@centos7 ~]# last |grep "^root\>"| tr -s " " | cut -d" " -f3 | grep "^[[:digit:]]" | sort -t. -k4 -n | uniq -c

1470460117810650.png

6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

0-9  :   [0-9]

10-99 :   [1-9][0-9]

100-199 :   1[0-9][0-9]

200-249 :   2[0-4][0-9]

250-255 :   25[0-5]

7、显示ifconfig命令结果中所有IPv4地址

[root@centos7 ~]# ifconfig | egrep -o "\<(([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])\>"

1470460158447113.png


课后作业

1、取本机ip地址

[root@localhost ~]# ifconfig | egrep -o "\<inet[[:space:]]*(([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])\>" | cut -d' ' -f2

Z1.png


2、取各分区利用率的数值

[root@localhost ~]# df | grep "^/dev/sd" | tr -s ' ' ':'| cut -d: -f1,5 | tr -d '%'

Z2.png

3、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

[root@localhost ~]# cat /etc/init.d/functions | tr -cs '[:alpha:]' "\n" | sort |uniq -c|  sort -n -r

Z3.png

4、正则表达式表示×××号

[1-9][0-9]{5}:前6位数字

(19[0-9][0-9]|200[0-9]|201[0-6]):4位年份

(0[0-9]|1[0-2]):2位月份

([0-2][0-9]|3[0-1]):2位日期

[0-9]{3}([0-9]|x):后四位随机数(包含出现的X情况)

[root@localhost ~]# echo "ID CARD :41071119940402301x" | egrep -o "\<[1-9][0-9]{5}(19[0-9][0-9]|200[0-9]|201[0-6])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])[0-9]{3}([0-9]|X)\>"

Z4.png

5、正则表达式表示手机号

[root@localhost ~]# echo "Phone number : 15836479198" | egrep -o "\<1(3|5|7|8)[0-9]{9}\>"

Z5.png

6、正则表达式表示邮箱

邮箱格式:用户名@服务器域名

[root@localhost ~]# grep -o "\<[[:alnum:]]\+@[[:alnum:]]\+\.com" mail

Z6.png

Z6.1.png

7、正则表达式表示QQ号

QQ号5-10位

[1-9]:第一个数字不能为0

[0-9]{4,9}:后面跟4-9位随机数

egrep -o "\<[1-9][0-9]{4,9}\>"

Z7.png