正則表達式練習題及作業(8.5)

當天練習題:

基本正則表達式練習題

1.顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式)   可有四種方式

[root@CentOS7 ~]# cat /proc/meminfo | grep -E "^(s|S)"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7320 kB
Slab:              74112 kB
SReclaimable:      28308 kB
SUnreclaim:        45804 kB
[root@CentOS7 ~]# cat /proc/meminfo | grep -E -i "^s"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7320 kB
Slab:              74112 kB
SReclaimable:      28308 kB
SUnreclaim:        45804 kB
[root@CentOS7 ~]# cat /proc/meminfo | grep -E "^[sS]"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7320 kB
Slab:              74112 kB
SReclaimable:      28308 kB
SUnreclaim:        45804 kB
[root@CentOS7 ~]# cat /proc/meminfo | grep -e "^s" -e "^S"
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7320 kB
Slab:              74112 kB
SReclaimable:      28308 kB
SUnreclaim:        45804 kB
[root@CentOS7 ~]#

2.顯示/etc/passwd文件中不以/bin/bash結尾的行

[root@CentOS6 ~]# grep -E -v "/bin/bash$" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
rtkit:x:499:499:RealtimeKit:/proc:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pulse:x:497:495:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nologin:x:510:514::/home/nologin:/sbin/nologin
[root@CentOS6 ~]#

3.顯示用戶rpc默認的shell程序

[root@CentOS6 ~]# grep -E "^rpc\>" /etc/passwd    #取出rpc用戶信息
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
[root@CentOS6 ~]# grep -E "^rpc\>" /etc/passwd | cut -d: -f7    #取出shell
/sbin/nologin
[root@CentOS6 ~]#

4.找出/etc/passwd中的兩位或三位數

[root@CentOS6 ~]# grep -o "\<[0-9]\{2,3\}\>" /etc/passwd
12
10
14
11
12
100
13
30
14
50
99
99
81
81
113
113
32
32
499
499
170
170
69
69
173
173
29
29
68
68
38
38
48
48
498
76
89
89
27
27
42
42
497
495
74
74
72
72
500
500
501
501
502
503
503
507
504
508
505
509
506
510
507
511
508
512
509
513
510
514
511
515
512
516
[root@CentOS6 ~]#

5.顯示/etc/grub.conf文件中,至少以一個空白字符開頭的且后面存非空白字符的行

[root@CentOS6 ~]# grep "^[[:space:]]\+[^[:space:]]" /etc/grub.conf 
    root (hd0,0)
    kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=f4d977bb-af76-4617-b07f-9c70ae42a056 rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-642.el6.x86_64.img
[root@CentOS6 ~]#

6.找出"netstat -tan"命令的結果中以'LISTEN'后跟0、1或多個空白字符結尾的行

[root@CentOS7 ~]# netstat -tan | grep -o "LISTEN[[:space:]]*$"
LISTEN     
LISTEN     
LISTEN     
LISTEN     
LISTEN     
LISTEN     
LISTEN     
[root@CentOS7 ~]#

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

[root@CentOS6 ~]# 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:507:511::/home/bash:/bin/bash
nologin:x:510:514::/home/nologin:/sbin/nologin
[root@CentOS6 ~]#

擴展正則表達式

1.顯示當前系統root、mage或wang用戶的UID和默認shell

[root@CentOS6 ~]# cat /etc/passwd | grep -E "^(root|mage|wang)\>"    #匹配出root、mage、wang三個用戶信息
root:x:0:0:root:/root:/bin/bash
mage:x:511:515::/home/mage:/bin/bash
wang:x:512:516::/home/wang:/bin/bash
[root@CentOS6 ~]# cat /etc/passwd | grep -E "^(root|mage|wang)\>" | cut -d: -f1,3,7    #取出用戶名、UID、shell
root:0:/bin/bash
mage:511:/bin/bash
wang:512:/bin/bash
[root@CentOS6 ~]#

2.找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行

[root@CentOS6 ~]# cat /etc/rc.d/init.d/functions | grep -E "[_[:alpha:]]+\(\)"
fstab_decode_str() {
checkpid() {
__readlink() {
__fgrep() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__umount_loop() {
__source_netdevs_fstab() {
__source_netdevs_mtab() {
__umount_loopback_loop() {
        __find_mounts() {
__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() {
action_silent() {
strstr() {
confirm() {
get_numeric_dev() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {
key_is_random() {
find_crypto_mount_point() {
init_crypto() {
[root@CentOS6 ~]#

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

[root@CentOS6 ~]# echo /etc/rc.d/init.d/functions/ | grep -E -o "[^/]+/?$"    #匹配以非斜線結尾的字符至少一次
functions/
[root@CentOS6 ~]# echo /etc/rc.d/init.d/functions/ | grep -E -o "[^/]+/?$" | grep -E -o "[^/]+"    #過濾掉結尾的/
functions
[root@CentOS6 ~]#

4.使用egrep取出上面路徑的目錄名

[root@CentOS6 ~]# echo /etc/rc.d/init.d/functions/ | grep -E -o "^/.*[^/]"    #首先過濾掉結尾的/
/etc/rc.d/init.d/functions
[root@CentOS6 ~]# echo /etc/rc.d/init.d/functions/ | grep -E -o "^/.*[^/]" | grep -E -o "^/.*/"    #取出目錄名
/etc/rc.d/init.d/
[root@CentOS6 ~]#

5.統計以root身份登錄的每個遠程主機IP地址的登錄次數

[root@CentOS7 ~]# last | grep -E -o "^root\>.*([[:digit:]]+\.){3}[[:digit:]]+" | tr -s ' ' | cut -d' ' -f1,3    #取出當前系統已root身份登錄的IP
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 172.18.19.139
root 192.168.0.107
root 192.168.0.109
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
root 10.1.250.60
[root@CentOS7 ~]# last | grep -E -o "^root\>.*([[:digit:]]+\.){3}[[:digit:]]+" | tr -s ' ' | cut -d' ' -f1,3 | sort | uniq -c    #取出重復次數
     22 root 10.1.250.60
      1 root 172.18.19.139
      1 root 192.168.0.107
      1 root 192.168.0.109
[root@CentOS7 ~]#

6.利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255

[root@CentOS6 ~]# grep "[0-9] [1-9][0-9] 1[0-9]{2} 2[0-4][0-9] 25[0-5]"

7.顯示ifconfig命令結果中所有IPv4地址

[root@CentOS6 ~]# 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.252.233
10.1.255.255
255.255.0.0
127.0.0.1
255.0.0.0
[root@CentOS6 ~]#

作業練習題:

1.取本機ip地址

[root@CentOS6 ~]# 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.252.233
10.1.255.255
255.255.0.0
127.0.0.1
255.0.0.0
[root@CentOS6 ~]#

2.取各分區利用率的數值

[root@CentOS6 ~]# df | tr -s ' ' | cut -d' ' -f5
Use%
10%
1%
19%
1%
[root@CentOS6 ~]#

3.統計/etc/init.d/functions 文件中每個單詞出現的次數,并按頻率從高到低顯示

[root@CentOS6 ~]# cat /etc/rc.d/init.d/functions | tr -sc '[:alpha:]' '\n' | sort | uniq -c | sort -nr -t' ' -k1
     83 if
     77 then
     75 pid
     73 echo
     72 fi
     61 return
     57 dev
     54 file
     50 n
     46 local
     42 kill
     39 z
     36 base
     35 remaining
     31 a
     30 d
     27 in
     25 null
     24 key
     23 is
     23 fstab
     23 done
     23 do
     23 awk
     22 pids
     22 list
     21 for
     21 BOOTUP
     20 to
     20 p
     20 dst
     19 shift
    ...

4.正則表達式表示身份證號

[root@CentOS7 ~]# egrep "\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|([0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>" number.txt 
210905197807210546 
370205197405213513 
372922198012224773 
370722197812222517
230803197906010035 
152801198703025310 
511428196305026357 
[root@CentOS7 ~]#

    

5.正則表達式表示手機號

[root@CentOS7 ~]# grep -E -o "(\+86)?1[38][0-9]{9}|14[57][0-9]{8}|15[0-35-9][0-9]{8}|17[0678][0-9]{8}" iphone.txt 
+8613868233891
15003107238
[root@CentOS7 ~]#

6.正則表達式表示郵箱

[root@CentOS7 ~]# egrep "\<([[:alnum:]]+(-|_)*[[:alnum:]]*)\>@([[:alnum:]]+\.)+[[:alnum:]]+" mail.txt 
993667135@qq.com
servername@163.com
[root@CentOS7 ~]#

7.正則表達式表示QQ號

[root@CentOS7 ~]# grep -E -o '\b[1-9][0-9]{4,12}\b' qq.txt 
333449
521521
796898
765423
93796117258
[root@CentOS7 ~]#

原創文章,作者:zhai796898,如若轉載,請注明出處:http://www.www58058.com/30996

(2)
zhai796898zhai796898
上一篇 2016-08-15
下一篇 2016-08-15

相關推薦

  • httpd2.2基礎安裝

    編譯安裝前,首先要確認開發包組已經裝上。 開發包組: Developments tools server platform development(centos 7) 并且將apr 與 apr-unil 裝上。這是httpd 可移植運行所必須用到的組件.這里舉例說明的是httpd2.2版本。 若是安裝httpd2.4以上版本的話,還要安裝pcre庫。pcre…

    2017-04-24
  • N25-第九周博客作業

    1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; #!/bin/bash # 統計登錄用戶和非登陸用戶的個數 # author: han declare -i loginnum=0 declare -i nologinnum=0 whil…

    Linux干貨 2017-02-24
  • week7

    1、創建一個10G分區,并格式為ext4文件系統; (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl; (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳; fdisk /dev/sdb  按提示創建出來10G分區 查看1:…

    Linux干貨 2016-12-08
  • Mariadb數據庫備份恢復系列(二):xtrabackup物理備份工具之完全備份

    實驗二:利用xtrabackup+二進制日志實現完全備份和恢復數據庫 1、安裝xtrabackup軟件包 2、驗證數據的存儲引擎類型 3、查看數據初始狀態 4、利用innobackupex進行完全備份 5、進行apply-log操作 6、查看備份出來的文件的信息 7、模擬在完全備份后,對數進行修改,以測試通過二進制日志還原完全備份后尚未來得及備份的變化的數據…

    Linux干貨 2016-11-24
  • 集中練習1-10個Linux運維腳本

    用戶管理、文本處理、文件管理相關

    2017-09-20
  • Linux用戶和組命令

                        groupadd命令用戶和工作組管理 groupadd命令用于創建一個新的工作組,新工作組的信息將被添加到系統文件中。-g:指定新建工作組的id;  -r:創建系統工作組,系統工作組的組ID小于500; -K:…

    2016-02-09
欧美性久久久久