课后习题3–正则表达式

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

[root@centos7 ~]# df | grep "^/dev" | grep -v "cdrom$" | tr ' ' ':' | tr -s ':' | cut -d: -f5 | sort   
1%
4%
73%

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

[root@centos7 ~]# cat /etc/passwd | sort -n -t: -k3 | cut -d: -f1,3,7 | tail -1
nfsnobody:65534:/sbin/nologin

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

[root@centos7 ~]# stat /tmp | head -4 | tail -1 | tr '(/)' ':' | tr ' ' ':' | tr -s ':' | cut -d: -f2
0777

或者

[root@centos7 ~]# stat /tmp | grep "^A.*)$" | tr ' ' '\n' | head -2 | tail -1 | tr -cd '[:digit:]'
0777

4、统计当前连接本机的每个远程主机IP的连接数(包括端口号),并按从大到小排序

[root@centos7 ~]# netstat -tan | grep "^tcp\>" | tr ' ' '*' | tr -s '*' | cut -d* -f4 | uniq -c | sort -r
      3 10.1.0.17:22
      1 192.168.122.1:53
      1 127.0.0.1:631
      1 127.0.0.1:25
      1 0.0.0.0:22

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

方法一

[root@centos7 ~]# grep "^[sS]" /proc/meminfo 
SwapCached:         6928 kB
SwapTotal:       2097148 kB
SwapFree:        2051836 kB
Shmem:             20884 kB
Slab:             150348 kB
SReclaimable:      84320 kB
SUnreclaim:        66028 kB

方法二

[root@centos7 ~]# grep -i "^s" /proc/meminfo
SwapCached:         6928 kB
SwapTotal:       2097148 kB
SwapFree:        2051836 kB
Shmem:             20884 kB
Slab:             150348 kB
SReclaimable:      84320 kB
SUnreclaim:        66028 kB

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

[root@centos7 ~]# grep -v "^/bin/bash$" /etc/passwd

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

[root@centos7 ~]# grep "^rpc\>" /etc/passwd | cut -d: -f1,3,7
rpc:32:/sbin/nologin

8、找出/etc/passwd中的两位或三位数,必须是正整数

[root@centos7 ~]# grep -E "\<1[0-9]{1,2}\>" /etc/passwd

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

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

10、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行

[root@centos7 ~]# netstat -tan | grep -E "LISTEN[[:space:]]*$" 
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN

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

[root@centos7 ~]# grep -E "^\<(.*)\>.*\<\1\>$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:4346:4346::/home/nologin:/sbin/nologin

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

[root@centos7 ~]# grep -E "^(root|mage|wang)\>" /etc/passwd | cut -d: -f1,3,7
root:0:/bin/bash
mage:4347:/bin/bash
wang:4348:/bin/bash

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

grep -E "^[[:alpha:]_]+\(\)" /etc/rc.d/init.d/functions

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

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

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

[root@centos7 ~]# echo /etc/rc.d/initd/function | grep -E -o "^/.*/"
/etc/rc.d/initd/

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

[root@centos7 ~]# ifconfig |grep -E -o  '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
10.1.0.17
255.255.0.0
10.1.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255

17、取出文本中的全部身份证号

[root@centos6 testdir]# grep -o -E "1[0-9]{17}" f1 
123456789123456789

18、取出文本中的全部手机号码

[root@centos6 testdir]# grep -o -E "1[0-9]{10}" f2
15335699718

19、取出文本中所有的邮箱地址

[root@centos6 testdir]# grep -o -E "([0-9]{5,}|[a-z])@([1-9]{1,}|[a-z]{1,}).com" f3
9687765@qq.com
12345@yahu.com
o@sina.com
o@163.com

附一个取目录名的方法,大家帮忙解释一下哈!

[root@centos7 ~]# echo /etc/rc.d/initd/function/ | grep -E -o "^/.*/\b"
/etc/rc.d/initd/

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

(1)
mfwingmfwing
上一篇 2016-08-07 22:05
下一篇 2016-08-07 22:05

相关推荐

  • 文件权限管理

    一 、文件属性介绍 1、linux文件权限分为读(read)、写(write)、执行(excute)三种,主要针对三类用户进行定义:ouwer:属主(u) 、group(g):属组、 other(o):其他人。文件的权限组合可以分为两种,一种是由三个字母r 、w、 x组成,一种是由数字组成4、2、1,它们代表的意思是r = 4 表示读权限、w = 2表示写权…

    Linux干货 2016-08-08
  • 进程,自动化任务与安全

    进程     进程就是一段存储了一个指令集的内存空间    相关命令:        pstree:显示当前运行的进程树,按树状结构显示     &n…

    Linux干货 2017-04-02
  • GOPS2017全球运维大会 • 深圳站将在深圳召开!

    第六届GOPS2017全球运维大会(本次)将于2017年4月21日-22日在深圳举行,历届金牌讲师精选亮相,各种精彩等您发掘。活动家为GOPS2017全球运维大会提供在线报名!在线报名地址:https://www.huodongjia.com/event-231365274.html 大会亮点 众多国外重量级嘉宾在路上 ► 目前正在和各位大咖商榷行程中,主会…

    2017-04-10
  • SRE管理职责简介

    读书笔记 摘要 SRE是Site Reliability Engineer的简称,从名字可以看出Google的SRE不只是做Operation方面的工作,更多是保障整个Google服务的稳定性。 SRE管理职责简介 监控系统 警报 工单 日志 应急事件处理 变更管理 预测需求和规划容量 资源部署 小结 监控系统 监控系统是 SRE 团队监控服务质量…

    Linux干货 2017-04-03
  • yum使用和源码编译进程

    yum 是通过分析RPM的标头数据,根据各软件的依赖关系制作出有依赖关系时的解决方案,然后自动处理软件的依赖性问题,以解决软件安装或升级的问题。  yum使用方法: yum [options] [command] [packages …] install package .. 通过yum安装软件包 update package .. 更新软件包 u…

    Linux干货 2016-08-24
  • linux上的LVM简明教程

    LVM是一个多才多艺的硬盘系统工具。在Linux上非常的好用,传统分区使用固定大小分区,重新调整大小十分麻烦。但是,LVM可以创建和管理“逻辑”卷,而不是直接使用物理硬盘。可以让管理员弹性的管理逻辑卷的扩大缩小,操作简单,而不损坏已存储的数据。可以随意将新的硬盘添加到LVM,以直接扩展已经存在的逻辑卷。 首先是实际的物理磁盘及其划分的分区和其上的物理卷(PV…

    Linux干货 2017-05-02