grep、egrep、正則表達式

 

總結本此課程中所涉及命令的使用方法及相關示例展示;

grep的使用

            grep: Global search REgular expression and Print out theline.

        作用:文本搜索工具,根據用戶指定的“模式(pattern)”逐行去搜索目標文本,打印匹配到的行;

        模式:由正則表達式的元字符及文本字符所編寫的過濾條件;

            元字符:字符不表示其字面意義,而用於表示通配或控制功能;

        分兩類:

            基本正則表達式:BRE

            擴展正則表達式:ERE

            正則表達式引擎;

    grep [OPTIONS] PATTERN [FILE...]

        選項:

            --color=auto:對匹配到的串做高亮顯示;

            -v:顯示模式匹配不到行;

            -i: 忽略字符大小寫;

            -o: 僅顯示能夠被模式匹配到的串本行;

            -q: 靜默模式;

            -E:使用擴展的正則表達式;

總結基本正則表達式及擴展正則表達式

字符匹配:

        .: 匹配任意單個字符;

        []:匹配指定範圍內的任意單個字符;

        [^]:匹配指定範圍內的任意單個字符;

[:lower:] 匹配小寫字母

    [:upper:] 匹配大寫字母

    [:alpha] 匹配所有字母

     [:digit:]匹配所有數字

    [:alnum:] 匹配所有數字字母 [:alpha:]和[:alpha:]和二爲一

    [:space] 代表空格字符 例如:tab,換行,空格之類的

[:punct:] 代表標點符號例如:'! " # $ %& ' ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ' { | }

 

次數匹配:用於要指定其次數的字符的後面;

                *: 任意次;

\?:0或1次;

                grep "x\?y"

                \+:1或多次;

                \{m\}:精確限制爲m次;

                \{m,n\}: 至少m次,至多n次,[m,n]

                    \{0,n\}:至多n次;

                    \{m,\}:至少m次;

.*: 匹配任意長度的任意字符;

位置錨定:

                ^: 行首錨定;用於模式的最左側;

                $: 行尾錨定;用於模式的最右側;

                \<, \b: 詞首錨定;用於表示單詞的模式的左側;

                \>, \b:詞尾錨定;用於表示單詞的模式的右側;

                ^$: 空白行;

            分組:\(\)

                分組的小括號中的模式匹配到的內容,會在執行過程中被正則表達式引擎記錄下來,並保存內置的變量中;這些變量分別是\1, \2, ...

                    \1: 從左側起,第一個左括號,以及與之配對的右括號中間的模式所匹配到的內容;

                    \2

                後向引用:使用變量引用前面的分組括號中的模式所匹配到的字符;

 

擴展的正則表達式:

        grep家庭有三個命令:

            grep:基本正則表達式

                -E: 擴展正則表達式

                -F:不支持正則表達式

            egrep:擴展正則表達式

            fgrep:不支持正則表達式

 

擴展正則表達式的元字符:

            字符匹配:

                .: 任意單個字符

                []:指定範圍內的任意單個字符

                [^]:指定範圍外的任意單個字符

            次數匹配:

                *匹配其前面的字符任意次

                ?: 0次或1次;

                +: 1次以上;

                {m}: 精確匹配m次;

                {m,n}: 至少m次,至多n次;

            錨定:

                ^: 錨定行首

                $: 錨定行尾

                \<, \b

                \>, \b

            分組:()

                後向引用:\1, \2, ...

            或者:

                a|b

                    C|cat: 不表示Cat或cat,而表示C或cat;

                        要寫成(C|c)at

 

                   練習:

1、  顯示/etc/passwd文件中以bash結尾的行;

[root@localhost~]# grep 'b..h$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost~]# grep .bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost~]# grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash

2、  顯示/etc/passwd文件中的兩位數或三位數;

[root@localhost~]# grep '[0-9]\{2,3\}' /etc/passwd
[root@localhost~]# grep --color=auto -E '\b[[:digit:]]{2,3}\b' /etc/passwd

3、  顯示'netstat-tan'命令結果中以‘LISTEN’後跟0個、1個或多個空白字符結尾的行;

[root@localhost~]# netstat -tan | grep --color=auto -E 'LISTEN[[:space:]]*$'
tcp        0     0 0.0.0.0:47425              0.0.0.0:*                   LISTEN    
tcp        0     0 0.0.0.0:111                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    
tcp        0     0 :::45984                   :::*                        LISTEN    
tcp        0     0 :::111                     :::*                        LISTEN    
tcp        0     0 :::22                      :::*                        LISTEN    
tcp        0     0 ::1:631                    :::*                        LISTEN    
tcp        0     0 ::1:25                     :::*                        LISTEN    
[root@localhost~]#

 

                            4、添加用戶bashtestbash,basher以及nologin用戶(nologin用戶的shell/sbin/nologin);而後找出/etc/passwd文件中用戶名同shell名的行;

 

[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd -s/sbin/nologin nologin
[root@localhost ~]# grep --color=auto -E'^([[:alnum:]]+):.*\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:500:500::/home/bash:/bin/bash
nologin:x:503:503::/home/nologin:/sbin/nologin

練習:

1、  顯示當前系統上rootcentosuser1用戶的默認的shellUID

[root@localhost ~]# useradd centos
[root@localhost ~]# useradd user1
[root@localhost ~]# egrep'^\<root|centos|user1\>' /etc/passwd | cut -d: -f3,7
0:/bin/bash
504:/bin/bash
505:/bin/bash
[root@localhost ~]#

2、  找出/etc/rc.d/init.d/functions文件中某單詞(單詞中間可以存在下劃線)後面跟着一組小括號的行;

[root@localhost ~]# grep -E'\<[0-9a-zA-Z_]+\>\(\)' /etc/rc.d/init.d/functions
[root@localhost ~]# egrep -E'\<[[:alpha:]_]+\>\(\)' /etc/rc.d/init.d/functions
[root@localhost ~]# egrep '[a-z].*\(\)'/etc/init.d/functions
[root@localhost ~]# egrep'[[:alpha:]].*\(\)' /etc/init.d/functions

                            3、使用echo輸出一個路徑,而後egrep找出其路徑基名;

 

                                     進一步地:使用egrep取出其目錄名;

路徑基名命令

[root@localhost ~]# echo/etc/sysconfig/iptables-config anaconda-ks.cfg |egrep -o '([^/]*)$'
iptables-config anaconda-ks.cfg
[root@localhost ~]#

路徑目錄名命令:

[root@localhost ~]# echo /etc/sysconfig/iptables-configanaconda-ks.cfg |egrep -o '^(.*)/'
/etc/sysconfig/
[root@localhost ~]#

 

3、  找出ifconfig命令執行結果中1-255之間的數字;

 

[root@localhost ~]# ifconfig |grep'[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' --colo

         inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0

         inet addr:127.0.0.1 Mask:255.0.0.0

 

[root@localhost ~]# ifconfig | grep--color=auto '\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)'

         inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0

         inet addr:127.0.0.1 Mask:255.0.0.0

 

[root@localhost ~]#  ifconfig | egrep --color=auto'\<([1-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

eth0     Link encap:Ethernet  HWaddr00:0C:29:D2:7E:71 

         inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0

         inet6 addr: fe80::20c:29ff:fed2:7e71/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

         RX bytes:187779 (183.3 KiB)  TXbytes:133085 (129.9 KiB)

         inet addr:127.0.0.1 Mask:255.0.0.0

         inet6 addr: ::1/128 Scope:Host

         UP LOOPBACK RUNNING MTU:65536  Metric:1

         RX packets:8 errors:0 dropped:0 overruns:0 frame:0

         TX packets:8 errors:0 dropped:0 overruns:0 carrier:0


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章