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

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

相关推荐

  • Docker 之初次体验

    一、Docker 简介  lxc linux container,openvz  容器中各虚拟机只有一个内核,而是多个用户空间  在库中完成虚拟化,比如wine 或者在windows中运行bash  在应用程序的运行级别提供虚拟化,比如jvm   pstree , pid 为1 的进程  …

    Linux干货 2015-01-16
  • 计划任务和周期性任务

    linux计划任务     功能:指定未来的某以特定的时间点执行一次任务     命令:at、bathc     at命令:         选项: &nbsp…

    Linux干货 2016-09-12
  • Linux的磁盘管理:

    Linux的磁盘管理: Linux的核心:一切介文件:     open,read,write,close 块设备:block,存取单位‘块’,磁盘 字符设备:char 存取单位‘字符’,键盘 设备文件:关联至一个设备驱动程序,进而能够跟与之对应硬件设备进行通信: 设备号码:   &nbs…

    Linux干货 2016-07-22
  • 内核编译实际效果演示

    内核编译实际操作效果演示     环境:CentOS7.2,自带内核版本为3.10.0-327.el7.x86_64,下载3.18.41版本进行编译     步骤1:确保开发工具包组已安装     [root@localhost …

    Linux干货 2016-09-13
  • hadoop安全模式

    hadoop安全模式在分布式文件系统启动的时候,开始的时候会有安全模式,当分布式文件系统处于安全模式的情况下,文件系统中的内容不允许修改也不允许删除,直到安全模式结束。安全模式主要是为了系统启动的时候检查各个DataNode上数据块的有效性,同时根据策略必要的复制或者删除部分数据块。运行期通过命令也可以进入安全模式。在实践过程中,系统启动的时候去修改和删除文…

    Linux干货 2015-04-13
  • N25第三周作业

    .列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登陆多次,则只显示一次即可。 此题主要考察命令who,cut,sort以及管道的基本用法:who:列出当前已登陆的用户名,登陆设备名,时间以及ip地址。 cut:    顾名思义就是截取之意, -d 指定要截取信息的分隔符,此处是以空格为分隔符,-f指定要截取的字段,此…

    Linux干货 2016-12-20