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.www58058.com/29751

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

相關推薦

  • 15 權限管理及作業

    15 權限管理及作業(作業單獨一篇) 一、雜項知識整理 1、訪問控制列表:ACL:Access Control List,實現靈活的權限管理。     除了文件的所有者,所屬組和其它人,可以對更多的用戶設置權限。     centos7.0之后默認創建的ext4文件系統有ACL功…

    Linux干貨 2016-08-04
  • Linux的啟動流程

    Linux的啟動流程大致上如下圖. 現在詳細說明一下每個步驟: 第一階段 當系統啟動時,系統首先會加載BIOS。BIOS的首先會檢查各硬件設備,當檢查完畢沒有問題之后。BIOS會根據設定的BootSequence來尋找可以引導系統的設備。一般而言,Linux是通過磁盤上MBR來引導系統的。 第二階段 MBR是Master Boot Record,是位于磁盤第…

    Linux干貨 2016-02-28
  • pxe和dhcp服務——引導安裝操作系統

    BootStraping:系統提供(OS Provision) pxe –> preboot excution environment, Intel cobbler –> Cobbler is a network install server.  Cobbler supports PXE, ISO virtual…

    Linux干貨 2016-11-05
  • linux基礎服務之DNS

    一、DNS簡介 1、DNS:Domain Name System(域名系統),是互聯網上IP和域名相互解析的分布式層級結構的數據庫。DNS的出現能夠使用戶更好的更加方便的訪問互聯網,不用記IP地址來訪問互聯網,可以通過人類更容易記住域名來訪問互聯網。 2、DNS是一種C/S架構的服務器,客戶機用于一個名字對應的地址,而服務器是為客戶機提供查詢的,查詢由兩種機…

    2017-05-29
  • 服務器故障的解決方法以及基本腳本的編寫

    1,當開機時一直重新啟動怎么辦? 在開機時就如字符界面后按下a鍵然后進入單用戶模式,通過設置 [root@CentOS6 boot]# vim /etc/inittab 這個文件,將里面的開機啟動項改為多用戶模式就可。 # id:3:initdefault: “/etc/inittab” 26L, 884C 講id這項設置完成后重新啟動就好。 2,忘記roo…

    Linux干貨 2017-05-15
  • Ansible+Corosync+Pacemaker+nfs實現http高可用

    目錄: (一)實驗環境 (二)準備工作 (三)為node1和node2配置基礎配置 (四)使用ansible部署nfs (五)使用ansible部署corosync和pacemaker (六)使用ansible安裝crmsh工具 (七)使用crmsh配置http高可用 (八)驗證 (九)需要注意的地方 (一)實驗環境 1.1、環境拓撲 1.2、所需系統 4臺…

    Linux干貨 2016-04-29
欧美性久久久久