課后習題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.www58058.com/30578

(1)
mfwingmfwing
上一篇 2016-08-07
下一篇 2016-08-07

相關推薦

  • 淺談DNS基本原理以及實現方法(二)

             DNS作為互聯網服務的一個基礎型服務,了解和掌握DNS服務的原理和配置將是每一個運維人員必備的技能,這就如同闖蕩江湖的俠客不但要有的招式,還要懂的運用這些招式的心法一樣,接下來我將從DNS的正反向解析、主從同步、子域授權以及Bind view這四個方面來揭開它神…

    Linux干貨 2015-12-19
  • Linux源程序包相關概念整理

    一、      Linux源程序包介紹 1)    linux源程序包基礎 1.         遵循常用開源協議:BSD、Apache Licence 2.0、GPL、LGPL、MIT 2.&nbs…

    系統運維 2015-09-22
  • Linux中賬號管理之權限管理(下)

    linux中的賬號管理我們在前面兩張已經介紹了一些用戶和組的相關概念,常用的配置文件,命令的使用?,F在我們來看看賬號管理中最傲嬌的部分就是我們的權限管理。 一、簡單介紹權限的概念 以install.log這個文件為例,查看install.log的元數據,從下圖可以看出,每個文件或者目錄都有它的所屬的主和所屬組,最左邊顯示不僅有它所屬類型,還有它的讀取寫入執行…

    Linux干貨 2016-08-08
  • 安裝CentOS6.8操作系統

        工具:     VMware Workstation 12     http://www.vmware.com/cn/products/workstation/workstation-evaluation.html  &n…

    Linux干貨 2016-08-04
  • N25期第七周作業

    創建一個10G分區,并格式為ext4文件系統   要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳 fdisk /dev/sdb # size 10G type 83 mke2fs -t ext4 -b 20…

    Linux干貨 2017-02-28
  • N27_網絡班第九周作業

    N27_網絡班第九周作業 1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現 #!/bin/bash loginuser=`grep -v ‘/sbin/nologin’ /etc/passwd | wc -l` nologin=`grep …

    2017-10-10
欧美性久久久久