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

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

相关推荐

  • 第十周-Centos启动流程及Shell脚本编程

    一、CentOS启动流程 POST –> Boot Sequence(BIOS) –> Boot Loader (MBR) –> Kernel(ramdisk) –> rootfs –> switchroot –> /sbin/init –…

    2017-10-14
  • mysql-yum安装多实例

    1.安装包 yum install mariadb-server 2.创建文件 3 分别生成3306,3307,3308数库文件 4.复制主配置文件并分别修改端口路径 5.准备一个启动脚本并开启服务 /mysqldb/3307/mysqld start 6.用mysql -S 命令进入    

    2018-01-28
  • CA服务器的搭建以及证书签署、dropbear的编译安装

    CA服务器的搭建以及证书签署、dropbear的编译安装 一、CA Server和Client: 1、CA server:创建私钥CA (1)   openssl的配置文件:/etc/pki/tls/openssl.conf   如果Client端的申请是来自不同的国家,则需要将下图中红色框内的三项,由“match”改为“opt…

    Linux干货 2016-09-23
  • 马哥教育网络21期+第三周练习博客

    马哥教育网络21期+第三周练习博客 一.回顾第二周的学习内容 1,Linux上的文件管理类的命令: mkdir:创建目录: [root@localhost ~]# mkdir /tmp/fstab2222 [root@localhost ~]# ls -l /tmp/ total&nbsp…

    Linux干货 2016-07-16
  • Linux系统文件查找locate和find命令工具使用

    讲到Linux的文件查找,首先大家一般在Windows中有过查找过文件,我们知道window是以文件名结尾来识别文件的,使用一些通配符*.doc,*.txt来检索一类文件,缩小范围,实现快速定位文件,在Linux中,也有文件查找的需要不过实现的方式将更加灵活; 1)locate工具  2)find工具 1.locate Linux中也有像windo…

    Linux干货 2016-08-18
  • I/O重定向及管道

    一、I/O重定向     程序=指定+数据     读取数据:input     输出数据:output     打开的文件都有一个fd:文件描述符     l…

    Linux干货 2016-08-02