正則表達式

正則表達式: 
            由一類特殊字符及文本字符所編寫的模式,其中有些字符(元字符)不表示字符
            字面意義,而表示控制或通配的功能
程序支持:  grep,sed,awk,vim,less,nginx,varnish 等

元字符分類:字符匹配,匹配次數,位置錨定,分組
man 7 regex 獲取幫助

. 匹配任意單個字符 (也可以ls a?   ls a?? ls *匹配任意一個單一字符)# echo adc |grep .

                                             # grep roo. /etc/passwd

正則表達式

[] 匹配指定范圍內的任意單個字符      # echo ac bc fc |grep [ab]c

                                                   # echo 1 2 3 4 5 6 3 7 fc |grep [1-5]

                                                   # echo 1 4 5 6 7 v 1l a b n |grep [1-6][a-l]
                                                  正則表達式

[^] 匹配指定范圍外的任意單個字符
[:alnum:] 或 [0-9a-zA-Z] # echo a d c b 1 2 3 | grep “[[:alnum:]]” # echo a d c b 1 2 3 ? | grep  [^[:alnum:]]
[:alpha:] 或 [a-zA-Z]
[:upper:] 或 [A-Z] 大寫字母 # echo a d c b 1 2 3 E ? | grep  “[[:digit:][:upper:]]”
[:lower:] 或 [a-z]
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:]包含的范圍廣)
[:cntrl:] 不可打印的控制字符(退格、刪除、警鈴…)
[:digit:] 十進制數字 或[0-9] # echo a d c b 1 2 3 | grep [[:digit:]]
[:xdigit:]十六進制數字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 標點符號

匹配次數: 用在要指定次數的字符的字符后面,用于指定前面的字符要出現的次數
            先編寫一個文件 cat > regex.txt 編寫內容
            匹配前面的字符任意任意次,包含0次 # cat regex.txt|grep g[o]le
               貪婪模式:盡可能長的匹配
            .任意長度的任意字符               # cat regex.txt|grep g.le
            \?匹配其前面的字符0或1次           # cat regex.txt|grep “g[o]\?gle”
            +匹配其前面的字符至少1次          # cat regex.txt|grep “g[o]+gle”
            {n}匹配其前面的字符n次           # cat regex.txt|grep “g[o]{4}gle”
            {m,n}匹配其前面的字符至少m次 至多n次 # cat regex.txt|grep “g[o]{1,10}gle”
            {,n}匹配其前面的字符 至多n次     # cat regex.txt|grep “g[o]{,10}gle”
            {n,}匹配其前面的字符 至少n次    # cat regex.txt|grep “g[o]{1,}gle”
        
             # cat regex.txt|grep g[o]le     gole
             # cat regex.txt|grep g[o][o]le  goole

            ^行首錨定,用于模式的最左側 # grep ^root /etc/passwd
            $行尾錨定,用于模式的最右側 # grep /bin/bash$ /etc/passwd
            ^PATTERN$用于模式匹配整行
                ^$空行 #cat /etc/httpd/conf/httpd.conf |grep -ve “^$” -e “^#”
                       #cat /etc/httpd/conf/httpd.conf |grep -v “^$|^#”
                ^[[:space:]]*$空白行
            \<或\b 詞首錨定,用于單詞模式的左側
            \>或\b 詞尾錨定,用于單詞模式的右側
            \<PATTERN\>匹配整個單詞
            # grep  “\<root\>” /etc/passwd
            # grep  “^root\>” /etc/passwd
            # grep  “^root\b” /etc/passwd

            # grep  “^root:\b” /etc/passwd

正則表達式

分組 :()將一個或多個字符捆綁在一起,當做一個整體進行處理 如: “(root)+”

           # grep  “(\root)” /etc/passwd
            
       \1表示從左側起第一個左括號以及與之匹配右括號之間的模式所匹配到的字符
         如: (string1+string2)
)
              \1:string1+(string2)
              \2 :string2
           寫一個腳本 test.txt
            #cat test.txt |grep “(r..t)”
            #cat test.txt |grep “(r..t).
\1″ 必須以root開頭并以root結尾 
             root,dig,raat,root,rooo 前四個顯示 必須以root開頭并以root結尾 
             root,dig,raat,rooo,root全部顯示  必須以root開頭并以root結尾 

       或者| a|: a或b C|cat: C或cat

練習:
1、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
        #cat /proc/meminfo|grep “^[Ss]”
        #cat /proc/meminfo|grep -i “^s”
        #cat /proc/meminfo|grep -e ^s -e ^S
        #cat /proc/meminfo|grep “^s|^S”
        #cat /proc/meminfo|grep “^[s|S]”
正則表達式
2、顯示/etc/passwd文件中不以/bin/bash結尾的行
        #grep -v “/bin/bash$” /etc/passwd

3、顯示用戶rpc默認的shell程序
        #grep “^rpc\>”   /etc/passwd  | cut -d : -f7 
        #grep -w “^rpc”   /etc/passwd  | cut -d : -f7 
正則表達式
4、找出/etc/passwd中的兩位或三位數
        #cat /etc/passwd |grep -o “\<[0-9]{2,3}\>”

正則表達式

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

        #cat /etc/grub2.cfg  |grep “^[[:space:]]\+[^[:space:]]”

正則表達式

6、找出“netstat -tan”命令的結果中以‘LISTEN’后跟任意多
個空白字符結尾的行

        #netstat -tan|grep “\<LISTEN\>[[:space:]]*$” 

正則表達式

7、顯示CentOS7上所有系統用戶的用戶名和UID
        #cat /etc/passwd |cut -d: -f1,3 |grep “\<[[:digit:]]{1,3}\>”$
正則表達式
8、添加用戶bash、testbash、basher、sh、nologin(其shell
為/sbin/nologin),找出/etc/passwd用戶名同shell名的行
        #cat /etc/passwd | grep “(^.)\>.\/\1$”
正則表達式
9、僅利用df和grep和sort,取出磁盤各分區利用率,并從大到小排序
        #df |grep ^/dev/sd |grep -o “\b[[:digit:]]{1,3}\b%”|sort -rn

正則表達式
擴展的正則表達式
egrep 或grep -E 可以省略\ 
  除了\<    \> 
       \b    \b
  不可以省略

作業:
1、顯示三個用戶root、mage、wang的UID和默認shell
       #cat /etc/passwd|grep -E “^(root|wang|mage)\>”|cut -d : -f3,7
       #cat /etc/passwd|grep -E -w “^(root|wang|mage)”|cut -d : -f3,7
正則表達式
2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行
      # cat /etc/rc.d/init.d/functions | egrep “^[[:alpha:]_]+\>()” 
      # cat /etc/rc.d/init.d/functions | grep  -o “^.*[:graph:]

      # cat /etc/rc.d/init.d/functions | grep  -o “^.*\>\(\)”

正則表達式

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

   # echo /etc/rc.d/init.d/functions |egrep “[^/]+/?$”

正則表達式

4、使用egrep取出上面路徑的目錄名
 # echo “/etc/rc.d/init.d/functions” |egrep -o ‘.*/\b’

 # echo “/etc/rc.d/init.d/functions” |egrep -o ‘.*/\<‘

正則表達式

5、統計last命令中以root登錄的每個主機IP地址登錄次數

# last |grep “^root\>”|egrep -o “([0-9]{1,3}\.){3}[0-9]{1,3}” |sort|uniq -c

正則表達式

6、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255
  #echo {0..300}|egrep -o “\<25[0-5]\>”
      [0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]
7、顯示ifconfig命令結果中所有IPv4地址

ifconfig | egrep -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])\>”

正則表達式
8、將此字符串:welcome to magedu linux 中的每個字符去重并排序,重復次數多的排到前面
      # echo “welcome to magedu linux”|grep -o . |sort|uniq -c |sort -nr
正則表達式

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

(0)
MOMOMOMO
上一篇 2017-08-05
下一篇 2017-08-05

相關推薦

  • LinuxGrub修復方法

    Linux因Grub損壞的修復方法: 1)救援模式修復(備份了MBR) 2)救援模式修復(無備份MBR) 3)Grub下手動啟動Linux系統 1、救援模式修復(備份了MBR)     MBR中存放了Bootloader信息(Grub),在磁盤的最開始512字節,當這512字節出現故障,系統將無法引導啟動。 &nbsp…

    Linux干貨 2016-06-22
  • shell編程之循環

           當需要重復運行特定的指令以滿足系統管理工作需要時,條件判斷語句if、case并不能很好地提供支撐,shell提供了for、while、until循環語句來滿足此需求。 一、for循環語句        for循環用于重復整個對象列表,依次遍歷對列…

    Linux干貨 2016-08-18
  • SHELL流程控制之循環

    當進行腳本編程時,語句執行的流程控制通常有三種: l  順序執行 l  選擇執行 l  循環執行   條件選擇if: if語句可以進行嵌套 if 判斷條件;then          條件為真的分支代碼 elif  判斷條件;th…

    Linux干貨 2016-08-18
  • 文件管理基礎知識及命令詳解

    文件系統     文件和目錄被組織成一個單根倒置樹結構     文件系統從根目錄下開始,用“ /”表示     根文件系統(rootfs): root filesystem    …

    Linux干貨 2016-08-05
  • DNS快速入門、正反向解析……

    DNS服務原理詳解 因特網上作為域名和IP地址相互映射的一個分布式數據庫,能夠使用戶更方便的訪問互聯網,而不用去記住能夠被機器直接讀取的IP數串。通過主機名,最終得到該主機名對應的IP地址的過程叫做域名解析。 我們在訪問一個網站的時候,只要輸入該網站的網址就會跳轉到該網站頁面,而實現這一過程就需要DNS服務器將域名解析為IP地址,進而實現數據通信。 DNS:…

    2017-06-03
欧美性久久久久