课后习题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
[email protected]
[email protected]
[email protected]
[email protected]

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

[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

相关推荐

  • N25-第三周作业

    第三周 一、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@zf ~]# who | cut -d " " -f1  | sort | uniq -…

    Linux干货 2016-12-20
  • Openssl加密解密原理+CA自建实现

     Openssl加密解密原理+CA自建实现     前言 互联网的惊人发展使企业和消费者都感到非常兴奋,它正改变着我们的生活和工作方式。但是,互联网的安全程度如何——尤其是在通过它发送机密信息时的安全性——已经成为人们关心的主要问题。随着时代的发展,加密原理也不断地在更新换代. 数据的加密目前已广泛地运用于战争,商业活…

    Linux干货 2015-05-25
  • 磁盘、文件系统管理

    linux磁盘、文件系统管理 硬盘:机械硬盘、固态硬盘     计算硬盘速度:IoPs:(Io  per  second ) 每秒20次 机械硬盘:     track:磁道     平均寻道时间  &nbsp…

    Linux干货 2016-08-25
  • 马哥教育网络班21期-第四周课程练习

    Do one thing at a time,and do well. 小僧近期忙的去尿尿的时间都要挤出来…..! 无人能理解 zZzz 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 编辑/etc/group文件,添加组hadoop。 手动编辑/etc/pass…

    Linux干货 2016-07-29
  • 优云携手网易云 助力企业“互联网+”转型

    12月16日,网易旗下“网易云”首届“网易云聚,共创未来”合作伙伴大会在网易杭州园区举行,优云(uyun.cn)成为首批授牌20家企业之一,并作为核心合作伙伴代表现场签署了合作伙伴协议。 右二为优云软件运营经理 柴勇 现场签署合作伙伴协议 会上,网易杭州研究院执行院长汪源介绍了网易云的战略生态全境,提出“产品体系+培训体系+服务体系”的网易云三大核心竞争力,…

    Linux资讯 2017-03-15
  • Linux系统下的翻译神器——Goldendict

    Linux系统下的翻译神器——Goldendict 学习Linux时明显感受到学习英文的重要性。绝大多数Linux的发行版英文版的功能要远强于中文。因此一款好的翻译软件是了解熟悉Linux系统的必需品。在Windows系统下有各种好用的词典程序,包括有道词典、bing词典、金山词霸等等,而这些软件都不能在linux下使用,即使能够使用也只是测试版,功能太少。…

    Linux干货 2017-04-24