擴展正則表達式及應用


第1章 擴展正則表達式

1.1  + 前一個字符連續出現了1次或1次以上

egrep  "0+" clsn.txt  1次或1次以上 >=1

egrep  "0*" clsn.txt  0次或0次以上 >=0

1.1.1 找到文本中的0

[root@znix ~]# egrep  "0+" clsn.txt

my qq num is 49000448.

not 4900000448.

[root@znix ~]# egrep -o  "0+" clsn.txt

000

00000

 

1.1.2 取出文件中的大寫字母

[root@znix ~]# grep -o "[A-Z]" clsn.txt

I

I

I

O

L

D

B

O

Y

1.1.3 取出連續出現的大寫字母

[root@znix ~]# egrep -o "[A-Z]+" clsn.txt

I

I

I

clsn

1.1.4 顯示所有的單詞

[root@znix ~]# egrep -o "[A-Za-z]+" clsn.txt

I

am

clsn

teacher

1.2  | 或者

表示找其中的一個或者是另外一個。

[root@znix ~]# egrep "clsn|oldbey" clsn.txt  -o

clsn

clsn

oldbey

/etc/services 中的兩個端口

[root@znix ~]# egrep "3306|1521" /etc/services

mysql           3306/tcp                        # MySQL

mysql           3306/udp                        # MySQL

ncube-lm        1521/tcp                # nCube License Manager

ncube-lm        1521/udp                # nCube License Manager

       找其中的A或者B或者C

[root@znix ~]# egrep "A|B|C" clsn.txt

my god ,i am not oldbey,but clsn!

       找到12或者56替換成空。

[root@znix ~]# echo 123456|sed -r 's#12|56##g'

34

1.3 () 小括號 反向引用

小括號里面的內容是一個整體,相當於是一個字符 

1.3.1 表示一個整體

[root@znix ~]# egrep "oldb(o|e)y" clsn.txt

I am clsn teacher!

my blog is http://clsn.blog.51cto.com

my god ,i am not oldbey,but clsn!

1.3.2 反向引用

       sed -r 使用擴展正則

[root@znix ~]# echo 123456|sed -r 's#..(..)..#\1#g'

34

       點表示任意一個字符,\2表示第二個括號。

[root@znix ~]# echo 123456|sed -r 's#(.).(..).(.)#\2#g'

34

1.4 {} 大括號(花括號)

0{n,m} 數字0連續出現了至少n,最多m

[root@znix ~]# egrep "[a-z]{3,6}" clsn.txt

I am clsn teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://clsn.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but clsn!

 

[root@znix ~]# egrep "[a-zA-Z]{3,6}" clsn.txt

I am clsn teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://clsn.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but clsn!
[root@znix ~]#

1.4.1 大括號的不同方法

 0{3,6}   >=3 <=6

 0{3}     ==3

 0{3,}    >=3

 0{,6}    >=0 <=6

1.5  

前一個字符連續出現了 0次或1

1.5.1 環境

[root@znix ~]# cat a.log

good

gd

god

goood

1.5.2 o連續出現0次或1

[root@znix ~]# egrep "gd|god" a.log

gd
god

[root@znix ~]# egrep "go?d" a.log

gd
god

1.6 正則表達式分類

1.6.1 基礎正則

^       以……開頭

$       以……結尾

^$     空行

.*      所有

[abc]   表示abc

[a-z]   表示az

[A-Z]   表示A-Z

[^abc]  表示排除abc

1.6.2 擴展正則表達式

+      連續出現 1次或1次以上

|       或者

()      小括號里面的內容是一個整體,相當於是一個字符

{}      0{n,m} 數字0連續出現了至少n,最多m

?       前一個字符連續出現了 0次或1

 

第2章 取出eth0網卡的ip地址

2.1 思路

1)先定位 取出第二行

2)取出ip地址

2.1.1 eth0的內容

[root@znix ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:A8:E4:14 

          inet addr:10.0.0.201  Bcast:10.0.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fea8:e414/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:14324203 (13.6 MiB)  TX bytes:26220378 (25.0 MiB)

2.2 方法一 sed 去頭去尾

sed命令,將其中不需要顯示的,逐步替換。

[root@znix ~]#  ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'|sed 's# .*$##g'

10.0.0.201

2.3 方法二sed 反向引用 

反向替換,使用()把ip地址保護起來,\1方向選擇,顯示出來ip

[root@znix ~]# ifconfig eth0|sed -nr '2s#^.*dr:(.*)  Bc.*$#\1#gp'

10.0.0.201

       簡寫👆

[root@znix ~]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*dr:(.*)Bc.*$#\1#g'

10.0.0.201 

2.4 方法三

使用把[^0-9.]之外的替換成空格,使用awk取出第一列。

[root@znix ~]# ifconfig eth0|sed -n '2s#[^0-9.]# #gp'|awk '{print $1}'

10.0.0.201

2.5 方法四

awk 指定分隔符,將空格和分號都定為分隔符,然后取列。

[root@znix ~]# ifconfig eth0|sed -n '2p'|awk -F "[ :]+" '{print $4}'

10.0.0.201

第3章 第三關練習題

3.1 如何取得/etiantian文件的權限對應的數字內容,如-rw-r--r--644,要求使用命令取得644 這樣的數字。

 

3.1.1 方法一 反向引用

使用反向引用,保護要取出的內容。

[root@znix ~]# stat /etc/services |sed -nr '4s#^.*\(0(.*)/-.*$#\1#gp'

644

3.1.2 方法二 掐頭去尾

兩個sed 將不需要的東西替換為空。

[root@znix ~]# stat /etc/hosts |sed -n '4s#^.*(0##gp'|sed 's#/.*$##g'

644

3.1.3 方法三 排除

[^0-7] 除了07以外的替換成空格

[root@znix ~]#  stat /etc/hosts |sed -n '4s#[^0-7]##gp'

064400

[root@znix ~]# stat /etc/hosts|sed -nr '4s#[^0-7]+# #gp'

 0644 0 0

 

第4章 特殊符號通配符

4.1 特殊符號

&&   並且 前面的執行對了執行后面

||   或者 前面命令執行失敗了再執行后面的

>>   追加輸出重定向

>   標准輸出重定向

/     路徑的分隔符

$    取變量的內容

.    當前目錄

..   當前目錄的上一級目錄

~    家目錄

|    管道

!    取反 find awk

#    注釋

4.2 通配符

* {}    找出文件

4.3 正則表達式(三劍客grep sed awk使用)

^

$

^$

.*

[abc]   一個整體 abc

        正則表達式認為只要是在中括號里面的就是一樣的.

[^abc]  abc之外


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM