课后习题3--正则表达式

前端之家收集整理的这篇文章主要介绍了课后习题3--正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

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

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

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

或者

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

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

  1. [root@centos7~]#netstat-tan|grep"^tcp\>"|tr'''*'|tr-s'*'|cut-d*-f4|uniq-c|sort-r
  2. 310.1.0.17:22
  3. 1192.168.122.1:53
  4. 1127.0.0.1:631
  5. 1127.0.0.1:25
  6. 10.0.0.0:22

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

方法

  1. [root@centos7~]#grep"^[sS]"/proc/meminfo
  2. SwapCached:6928kB
  3. SwapTotal:2097148kB
  4. SwapFree:2051836kB
  5. Shmem:20884kB
  6. Slab:150348kB
  7. SReclaimable:84320kB
  8. SUnreclaim:66028kB

方法

  1. [root@centos7~]#grep-i"^s"/proc/meminfo
  2. SwapCached:6928kB
  3. SwapTotal:2097148kB
  4. SwapFree:2051836kB
  5. Shmem:20884kB
  6. Slab:150348kB
  7. SReclaimable:84320kB
  8. SUnreclaim:66028kB

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

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

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

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

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

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

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

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

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

  1. [root@centos7~]#netstat-tan|grep-E"LISTEN[[:space:]]*$"
  2. tcp00192.168.122.1:530.0.0.0:*LISTEN
  3. tcp000.0.0.0:220.0.0.0:*LISTEN
  4. tcp00127.0.0.1:6310.0.0.0:*LISTEN
  5. tcp00127.0.0.1:250.0.0.0:*LISTEN
  6. tcp600:::22:::*LISTEN
  7. tcp600::1:631:::*LISTEN
  8. tcp600::1:25:::*LISTEN

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

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

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

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

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

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

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

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

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

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

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

  1. [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])'
  2. 10.1.0.17
  3. 255.255.0.0
  4. 10.1.255.255
  5. 127.0.0.1
  6. 255.0.0.0
  7. 192.168.122.1
  8. 255.255.255.0
  9. 192.168.122.255


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

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

猜你在找的正则表达式相关文章