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

文本处理工具(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 Bash Shell有关变量

    bash 创建一个子bash-n 查看是否有错误-x 逐行显示信息$:显示当前PIDPPID :显示父进程PIDSHLVL :显示当前shell是几级shellpstree 显示进程树-p 同时显示进程号ps -ef 显示进程信息 本地变量 :仅对当前SHELL有效,对子SHELL也无效环境变量:对当前SHELL及子SHELL均有效。 set 显示本地变量和…

    2017-09-16
  • NET25-第11周作业

    1、详细描述一次加密通讯的过程,结合图示最佳。 第一步:他们需要实现协商好对称加密算法,单向加密算法,公钥加密算法,交换公钥等。 第二步:B用户想要将数据传给A,首先需要使用单向加密算法取出数据的特征码,并用自己的私钥对这段特征码进行加密(数字签名),B用户生成临时对称密钥,并用对称密钥加密整段数据,B用户使用 A用户的公钥加密一次性对称密钥,附加在整段数据…

    Linux干货 2017-05-15
  • 学习宣言

    努力不只是为了更好的生活,更是为了证明自己! 只有逼自己一把,才能知道自己是可以做到的! 路漫漫其修远兮,吾将上下而求索。

    Linux干货 2016-12-26
  • 常见HTTP code大全

    1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码。 代码 说明 100 (继续) 请求者应当继续提出请求。服务器返回此代码表示已收到请求的第一部分,正在等待其余部分。  101 (切换协议) 请求者已要求服务器切换协议,服务器已确认并准备切换。 2xx (成功)表示成功处理了请求的状…

    Linux干货 2016-09-19
  • dd工具

    dd     dd命令:convert and copy a file     用法:        ddif=/PATH/FROM/SRC of=/PATH/TO/DEST       &…

    Linux干货 2016-09-02
  • 一些基础命令的基本用法介绍

    节选一部分常用参数用法 基础命令 tty 切换X Window或命令行模式 Ctrl+Alt+F1~F6  tty1~tty6文字界面 Ctrl+Alt+F7  图形界面 startx 登录图形界面系统 date 日期 date +%Y/%m/%d  当前日期 date +%H:…

    Linux干货 2016-10-31