M20-1正则表达式有话说

1、找出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])'
192.168.226.133
255.255.255.0
192.168.226.255
10.1.249.40
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
[root@centos7 ~]#
[root@centos7 ~]# ifconfig | grep -o -E '\<([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])\>'
255.255.255.0
10.1.249.40
255.255.0.0
10.1.255.255
255.0.0.0
255.255.255.0
[root@centos7 ~]# ifconfig|egrep -o '(\<([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])\>'
255.255.255.0
10.1.249.40
255.255.0.0
10.1.255.255
255.0.0.0
255.255.255.0
[root@centos7 ~]#

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

root@centos7 ~]# df|tail -7|tr -s ' ' ':'
/dev/sda3:123157756:3851192:119306564:4%:/
devtmpfs:486124:0:486124:0%:/dev
tmpfs:500664:12:500652:1%:/dev/shm
tmpfs:500664:26036:474628:6%:/run
tmpfs:500664:0:500664:0%:/sys/fs/cgroup
/dev/sda1:508588:157108:351480:31%:/boot
tmpfs:100136:0:100136:0%:/run/user/0
[root@centos7 ~]# df|tail -7|tr -s ' ' ':'|cut -d: -f5
4%
0%
1%
6%
0%
31%
0%
[root@centos7 ~]# df|tail -7|tr -s ' ' ':'|cut -d: -f5 |sort -rn |head -1
31%
[root@centos7 ~]#

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

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

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

[root@centos7 ~]# stat /tmp|head -4|tail -1 |tr -d '[:punct:][:alpha:]'|tr -s ' ' ":"
:1777:0:0:
[root@centos7 ~]# stat /tmp|head -4|tail -1 |tr -d '[:punct:][:alpha:]'|tr -s ' ' ":"|cut -d: -f2
1777
[root@centos7 ~]#

5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@centos7 ~]# netstat -tn|grep tcp |tr -s  ' ' ':' |cut -d: -f4|uniq -c
      4 192.168.226.133
[root@centos7 ~]#

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

[root@centos7 ~]# grep "^[sS].*" /proc/meminfo
SwapCached:            4 kB
SwapTotal:       2098172 kB
SwapFree:        2098168 kB
Shmem:             26060 kB
Slab:             118472 kB
SReclaimable:      68372 kB
SUnreclaim:        50100 kB
[root@centos7 ~]#
[root@centos7 ~]# grep -i "^s.*" /proc/meminfo
SwapCached:            4 kB
SwapTotal:       2098172 kB
SwapFree:        2098168 kB
Shmem:             26060 kB
Slab:             118472 kB
SReclaimable:      68372 kB
SUnreclaim:        50100 kB
[root@centos7 ~]#

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

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

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

[root@centos7 ~]# grep "^rpc\>" /etc/passwd |cut -d: -f7
/sbin/nologin
[root@centos7 ~]#

9、找出/etc/passwd中的两位或三位数 

[root@centos7 ~]# grep -E "\<[1-9][0-9]\>|\<[1-9][0-9][0-9]\>" /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin
[root@centos7 ~]# grep "\<[1-9][0-9]\{1,2\}\>" /etc/passwd 
[root@centos7 ~]# grep -w "[1-9][0-9][0-9]\?" /etc/passwd

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

grep "^[[:space:]]\+[^[:space:]].*" /etc/grub2.cfg

11、找出"netstat -tan"命令的结果中以'LISTEN'后跟多个空白字符的行 

[root@centos7 ~]# netstat -tan |grep "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
[root@centos7 ~]#

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

root@centos7 ~]# grep "^\<\(.*\)\>.*\<\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
bash:x:1007:1007::/home/bash:/bin/bash
nologin:x:1009:1009::/home/nologin:/sbin/nologin
[root@centos7 ~]# grep "^\<\(.*\)\>.*/\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
bash:x:1007:1007::/home/bash:/bin/bash
nologin:x:1009:1009::/home/nologin:/sbin/nologin
[root@centos7 ~]#

13、显示当前系统root、mage或wang用户的UID和默认shell 

root@centos7 ~]# grep -E "^\<root\>|^\<mage\>|^\<wang\>"  /etc/passwd
root:x:0:0:root:/root:/bin/bash
wang:x:1003:10011::/home/wang:/bin/bash
mage:x:1010:1010::/home/mage:/bin/bash
[root@centos7 ~]# grep -E "^\<root\>|^\<mage\>|^\<wang\>"  /etc/passwd |cut -d: -f3,7
0:/bin/bash
1003:/bin/bash
1010:/bin/bash
[root@centos7 ~]#
[root@centos7 ~]# grep -E '^(mage|wang|root)\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
wang:x:1003:10011::/home/wang:/bin/bash
mage:x:1010:1010::/home/mage:/bin/bash
[root@centos7 ~]# grep -E '^(mage|wang|root)\>' /etc/passwd |cut -d: -f3,7
0:/bin/bash
1003:/bin/bash
1010:/bin/bash
[root@centos7 ~]#

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

[root@centos7 ~]# grep -E  '^[[:alpha:]_]+\(\)' /etc/init.d/functions
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {
[root@centos7 ~]#

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

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

未完结,待续,正则正忙,小编去喝茶了^_^。。。。。:)

本章节总结在如下博客地址欢迎访问:http://purify.blog.51cto.com/

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

(0)
alrenalren
上一篇 2016-08-05
下一篇 2016-08-06

相关推荐

  • 第三周课堂练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@myserver ~]# whoami  root  [root@myserver ~]# who | cut -d' ' -…

    Linux干货 2016-09-19
  • 基于ssl的mysql的主从复制

    实验环境: system:CentOS Linux release 7.2.1511 (Core) mariadb server:mariadb-server-5.5.44-2.el7.centos.x86_64 master server:10.1.51.20/16 slave serv…

    2016-11-22
  • HA Cluster:keepalived

    HA Cluster 集群类型:LB(lvs/nginx(http/upstream, stream/upstream))、HA、HP 系统可用性的公式:A=MTBF/(MTBF+MTTR) 系统故障: 硬件故障:设计缺陷、wear out、自然灾害 软件故障:设计缺陷 提升系统高用性的解决方案之降低MTTR: 解决方案:冗余(redundant) acti…

    Linux干货 2017-06-26
  • 第九周shell脚本编程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; 2、写一个脚本 (1) 获取当前主机的主机名,保存于hostname变量中; (2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改www.magedu.com; (3…

    2017-11-23
  • raid介绍

    raid介绍 在生产环境中,服务器所需的存储容量比较大,一般都会选择磁盘阵列来走存储。 阵列的优点 多个磁盘合成一个“阵列”来提供更好的性能、冗余,或者两者都提供 首先在I/O能力上,多个磁盘并行读写,可以提高读写的速度。 阵列所提供的磁盘冗余,提升存储设配的耐用性。 RAID实现的 外接式磁盘阵列:通过扩展卡提供适配能力 内接式RAID:主板集成RAID控…

    2017-06-24
  • 第三周博客作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 ~]# who | cut -d' ' -f 1 |sort |uniq -c 2、取出最后登录到当前系统的用户的相关信息。 ~]# last | head -1 3、取出当前系统上被用户当作其默认shell的最多的那个shell。 ~]# cat…

    Linux干货 2016-12-14