文本处理工具(练习+作业)

文本处理工具(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 : 18888888888" | 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

原创文章,作者:Groot,如若转载,请注明出处:http://www.178linux.com/30027

(0)
GrootGroot
上一篇 2016-08-07 22:05
下一篇 2016-08-07 22:05

相关推荐

  • linux系统用户管理和grep正则表达式练习

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@suywien ~]# cp -rpv /etc/skel/ /home/tuser1/ ‘/etc/skel/’ -> ‘/home/tuser1/’ ‘/etc/skel/.mozilla’ ->…

    Linux干货 2018-03-25
  • 第一周作业

    1、描述计算机的组成及其功能。     运算器,控制器,存储器,输入设备,输出设备     CPU:运算器+控制器     内存,硬盘:存储器     输入设备:键盘鼠标   &n…

    Linux干货 2016-06-26
  • 对软连接进行cp,rm

    软连接的原理 图1 由 1 号inode 读取到连结档的内容仅有档名,根据档名链接到正确的目录去取得目标文件的inode , 最终就能够读取到正确的数据了。 软连接的使用 创建软连接,从下图可以看出软连接的大小是11个字节,对比/etc/passwd和passwdlns可知他们不是同一个文件。 在复制cp的时候对软连接的处理 对指向文件的的软连接:使用cp …

    Linux干货 2017-02-25
  • Linux磁盘管理

    设备文件 一切皆文件 open(), read(), write(), close() 磁盘结构 磁盘接口类型 并行 IDE:133MB/s SCSI:640MB/s 串口 SATA:6Gbps SAS:6Gbps USB:480MB/s rpm rotationsper minute 硬盘每分钟转数 硬盘结构  图一…

    Linux干货 2016-08-30
  • 源码编译安装http

    为什么需要编译安装软件?   1、软件在编译期间需要配置:比如需要指定安装路径,定制模块等功能;   2、软件需要统一安装路径:在编译安装时可以方便指定这些路径;   3、需要最新的版本:对于某些软件来说可能需要最新的版本。 编译前的准备工作:     1、安装开发工具:make、gcc等…

    Linux干货 2016-08-24
  • LINUX-初学正则表达式

    正则表达式    简介       REGEXP:由一类特殊字符及文本字符由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)                    不表示字符字面意义,而表示控制或通配…

    2017-06-04