课后习题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)
上一篇 2016-08-07 22:05
下一篇 2016-08-07 22:05

相关推荐

  • N25第三周作业(用户组,和文本管理)

    列出当前系统 上所有已经登录用户的用户名,注意:同一个用户登录多次,则只显示一次即可. 列出最后登录到当前系统的用户相关信息. 命令who查看所有用户 , tail查看后几行 取出当前系统上被用户当作其默认shell的最多那个shell. 命令cut 分割 , -d 指定分隔符,-f指定字段 uniq 显示或忽略重复行信息   -c:显示并统计重复…

    Linux干货 2016-12-19
  • Homework Week-6 vim使用、脚本编程

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; cp /etc/rc.d/rc.sysinit /tmp/ vim /tmp/rc.sysinit 转换为末行模式: :%s@^[[:s…

    Linux干货 2016-09-19
  • Linux之磁盘管理

    Linux之磁盘管理       linux系统中, 一切都是文件, 而这些文件都是存储在磁盘中, 因此对于磁盘的管理是非常重要的, Linux磁盘管理的好坏, 直接关系到整个系统的性能问题, 本文主要介绍了磁盘结构, 分区类型, 管理分区, 管理文件系统, 挂载设备, 管理虚拟内存这几大项的内容.    &…

    Linux干货 2016-08-29
  • Linux交换分区的创建和调整

    概述 本文对swap进行简要介绍,以及系统安装后(在运行中)如何建立或调整swap分区。 文中演示环境为: 什么是swap space? 交换分区,即swap space。它将某段磁盘空间独立划分出来,当物理内存不够的情况下,系统先把物理内存中暂时不用的数据,暂存到交换空间,腾出内存空间来,以保证程序的运行;当swap中存储的页面被访问到时,系统会将其重新载…

    Linux干货 2016-08-30
  • LINUX下用户管理命令简述

    LINUX下用户管理命令简述 添加用户并设置密码 useradd [用户名] 创建用户 [root@localhost ~]# useradd jack [root@localhost ~]# cat /etc/shadow | grep jack jack:!!:17257:0:99999:7::: passwd [用户名] 设置密码 [root@loca…

    Linux干货 2017-04-05
  • 文件查找与压缩

    文件查找与压缩   Locate:非事实查找(数据库查找)     查询系统上预建的文件索引数据库 /var/lib/mlocate/mlocate.db 依赖于事先构建的索引; 索引的构建是在系统较为空闲时自动进行(周期性任务); 管理 员手动更新数据库(updatedb);   工作特点: 查找…

    Linux干货 2016-08-18