课后习题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
下一篇 2016-08-07

相关推荐

  • 走进Linux(二)

    Linux基础知识 1、文件管理类命令 mkdir:创建空目录 格式 mkdir [选项](可跟多个选项) 要创建的目录名 选项 -p:递归创建多个目录                             &nbsp…

    Linux干货 2016-09-26
  • 任务计划管理

    一:单一工作调度:at命令       列出在指定的时间和日期在计算机上运行的已计划命令或计划命令和程序。必须正在运行“计划”服务才能使用 at 命令。 示例: [root@CentOS 6 ~]#/etc/init.d/atd restart   启动服务 …

    Linux干货 2016-09-12
  • 第七周作业

    查看作业内容请移步此链接:http://www.cnblogs.com/wangenzhi/p/6403568.html

    Linux干货 2017-02-15
  • FHS文件系统共能介绍

    FHS文件系统共能介绍   FHS,即File Hierarchy Standard,文件层级标准。多数Linux系统下的文件管理采用此种文件组织形式,它定义了系统中每个区域的用途、所需要的最小构成的文件和目录,同时还给出了例外处理与矛盾处理。   这中组织是一种倒树状结构,所有的文件与目录都是由根目录”/”开始,然后…

    Linux干货 2016-10-18
  • The second week’s homework

    一.linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 Linux 文件与目录管理 我们知道Linux的目录结构为树状结构,最顶级的目录为根目录 /。 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们。 首先需要知道什么是绝对路径与相对路径。 绝对路径: 路径的写法,由根目录 / 写起,例如: /usr/shar…

    Linux干货 2016-12-12
  • java基础篇—内存分析

    Java的并发采用的是共享内存模型(而非消息传递模型),线程之间共享程序的公共状态,线程之间通过写-读内存中的公共状态来隐式进行通信。多个线程之间是不能直接传递数据交互的,它们之间的交互只能通过共享变量来实现 同步是显式进行的。程序员必须显式指定某个方法或某段代码需要在线程之间互斥执行。 1、多线程通信 1.1 内存模型 Java线程之间的通信由Java内存…

    Linux干货 2015-03-16