博文目錄
在望城等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都做網(wǎng)站、網(wǎng)站設計 網(wǎng)站設計制作按需制作網(wǎng)站,公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,品牌網(wǎng)站建設,營銷型網(wǎng)站,外貿(mào)網(wǎng)站制作,望城網(wǎng)站建設費用合理。
一、正則表達式的定義
二、擴展正則表達式元字符
三、文本處理器
正則表達式又稱正規(guī)表達式、常規(guī)表達式。在代碼中常簡寫為regex、regexp或RE。正則表達式是使用單個字符串來描述,匹配一系列符合某個句法規(guī)則的字符串,簡單來說,是一種匹配字符串的方法,通過一些特殊符號,實現(xiàn)快速查找、刪除、替換某個特定字符串。
正則表達式是由普通字符與元字符組成的文字模式。模式用于描述在搜索文本時要匹配的一個或多個字符串。正則表達式作為一個模板,將某個字符模式與所搜索的字符串進行匹配。其中普通字符包括大小寫字母、數(shù)字、標點符號及一些其他符號,元字符則是指那些在正則表達式中具有特殊意義的專用字符,可以用來規(guī)定其前導字符(即位于元字符前面的字符)在目標對象中的出現(xiàn)模式。
正則表達式的字符串表達方法根據(jù)不同的嚴謹程度與功能分為基本正則表達式與擴展正則表達式?;A正則表達式是常用的正則表達式的最基礎的部分。在Linux系統(tǒng)中常見的文件處理工具中grep與sed支持基礎正則表達式,而egrep與awk支持擴展正則表達式。
提前準備一個名為test.txt的測試文件,文件具體內(nèi)容如下:
[root@centos01 ~]# vim test.txt
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.14148223023840-2382924893980--2383892948
a wood cross!
Actions speak louder than words
#wooood #
#woooood #
AxyzxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
[root@centos01 ~]# grep -n 'the' test.txt
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
[root@centos01 ~]# grep -in 'the' test.txt
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
[root@centos01 ~]# grep -vn 'the' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
7:PI=3.14148223023840-2382924893980--2383892948
8:a wood cross!
9:Actions speak louder than words
10:
11:
12:#wooood #
13:#woooood #
14:AxyzxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
[root@centos01 ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
[root@centos01 ~]# grep -n 'oo' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#wooood #
13:#woooood #
15:I bet this place is really spooky late at night!
[root@centos01 ~]# grep -n '[^w]oo' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
12:#wooood #
13:#woooood #
15:I bet this place is really spooky late at night!
[root@centos01 ~]# grep -n '[^a-z]oo' test.txt
3:The home of Football on BBC Sport online.
[root@centos01 ~]# grep -n '[0-9]' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.14148223023840-2382924893980--2383892948
[root@centos01 ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
[root@centos01 ~]# grep -n '^[a-z]' test.txt
1:he was short and fat.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
8:a wood cross!
[root@centos01 ~]# grep -n '^[A-Z]' test.txt
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
6:The year ahead will test our political establishment to the limit.
7:PI=3.14148223023840-2382924893980--2383892948
9:Actions speak louder than words
14:AxyzxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
[root@centos01 ~]# grep -n '^[^a-zA-Z]' test.txt
12:#wooood #
13:#woooood #
[root@centos01 ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
[root@centos01 ~]# grep -n 'ooo*' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
13:#woooooood #
19:I bet this place is really spooky late at night!
[root@centos01 ~]# grep -n 'woo*d' test.txt
8:a wood cross!
11:#woood #
13:#woooooood #
[root@centos01 ~]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
[root@centos01 ~]# grep -n 'o\{2\}' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
13:#woooooood #
19:I bet this place is really spooky late at night!
在Linux/UNIX系統(tǒng)中包含很多種文本處理器或文本編輯器,其中包括VIM編輯器與grep等。而grep,sed,awk更是shell編程中經(jīng)常用到的文本處理工具,被稱為shell編程三劍客。
sed(Stream EDitor)是一個強大而簡單的文本解析轉換工具,可以讀取文本,并根據(jù)指定的條件對文本內(nèi)容進行編輯(刪除、
替換、添加、移動等),最后輸出所有行或者僅輸出處理的某些行。sed也可以在無交互的情況下實現(xiàn)相當復雜的文本處理操作,被廣泛應用于shell腳本中,用以完成各種自動化處理任務。
sed的工作流程主要包括讀取、執(zhí)行和顯示三個過程:
- 讀?。簊ed從輸入流(文件、管道、標準輸入)中讀取一行內(nèi)容并存儲到臨時的緩沖區(qū)中(又稱模式空間,patterm space)。
- 執(zhí)行:默認情況下,所有的sed命令都在模式空間中順序地執(zhí)行,除非指定了行的地址,否則sed命令將會在所有的行上依次執(zhí)行。
- 顯示:發(fā)送修改后的內(nèi)容到輸出流。再發(fā)送數(shù)據(jù)后,模式空間將會被清空。在所有的文件內(nèi)容都被處理完成之前,上述過程將重復執(zhí)行,直到所有內(nèi)容被處理完。
sed[選項] '操作' 參數(shù)
sed [選項] -f scriptfile 參數(shù)
常見的sed命令選項主要包含以下幾種:
- -e或--expression=:表示用指定命令或者腳本來處理輸入的文本文件。
- -f或--file=:表示用指定的腳本文件來處理輸入的文本文件。
- -h或--help:顯示幫助。
- -n、--quiet或silent:表示僅顯示處理后的結果。
- -i:直接編輯文本文件。
“操作”用于指定對文件操作的動作行為,也就是sed的命令。通常情況下是采用的“[n1[,n2]]”操作參數(shù)的格式。n1、n2是可選的,不一定會存在,代表選擇進行操作的行數(shù),如操作需要在5~20行之間進行,則表示為“5,20動作行為”。常見的操作包括以下幾種:- a:增加,在當前行下面增加一行指定內(nèi)容。
- c:替換,將選定行替換為指定內(nèi)容。
- d:刪除,刪除選定的行。
- i:插入,在選定行上面插入一行指定內(nèi)容。
- p:打印,如果同時指定行,表示打印指定行;如果不指定行,則表示打印所有內(nèi)容;如果有非打印字符,則以ASCII碼輸出。其通常與“-n”選項一起使用。
- s:替換,替換指定字符。
- y:字符轉換。
[root@centos01 ~]# sed -n '3p' test.txt
The home of Football on BBC Sport online.
[root@centos01 ~]# sed -n '3,5p' test.txt
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
[root@centos01 ~]# sed -n 'p;n' test.txt
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
PI=3.141592653589793238462643383249901429
Actions speak louder than words
#woood #
#woooooood #
I bet this place is really spooky late at night!
I shouldn't have lett so tast.
[root@centos01 ~]# sed -n 'p;n' test.txt
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
PI=3.141592653589793238462643383249901429
Actions speak louder than words
#woood #
#woooooood #
I bet this place is really spooky late at night!
I shouldn't have lett so tast.
[root@centos01 ~]# sed -n '1,5{p;n}' test.txt
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
[root@centos01 ~]# sed -n '10,${n;p}' test.txt
#woood #
#woooooood #
I bet this place is really spooky late at night!
I shouldn't have lett so tast.
[root@centos01 ~]# sed -n '/the/p' test.txt
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
[root@centos01 ~]# sed -n '4,/the/p' test.txt
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
[root@centos01 ~]# sed -n '/the/=' test.txt
4
5
6
[root@centos01 ~]# sed -n '/^PI/p' test.txt
PI=3.141592653589793238462643383249901429
[root@centos01 ~]# sed -n '/\/p' test.txt
a wood cross!
[root@centos01 ~]# nl test.txt | sed '3d'
1 he was short and fat.
2 He was wearing a blue polo shirt with black pants.
4 the tongue is boneless but it breaks bones.12!
5 google is the best tools for search keyword.
6 The year ahead will test our political establishment to the limit.
7 PI=3.141592653589793238462643383249901429
8 a wood cross!
9 Actions speak louder than words
10
11 #woood #
12
13 #woooooood #
14
15
16 AxyzxyzxyzxyzC
17
18
19 I bet this place is really spooky late at night!
20 Misfortunes never come alone/single.
21 I shouldn't have lett so tast.
[root@centos01 ~]# nl test.txt | sed '3,5d'
1 he was short and fat.
2 He was wearing a blue polo shirt with black pants.
6 The year ahead will test our political establishment to the limit.
7 PI=3.141592653589793238462643383249901429
8 a wood cross!
9 Actions speak louder than words
10
11 #woood #
12
13 #woooooood #
14
15
16 AxyzxyzxyzxyzC
17
18
19 I bet this place is really spooky late at night!
20 Misfortunes never come alone/single.
21 I shouldn't have lett so tast.
[root@centos01 ~]# sed '/^[a-z]/d' test.txt
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
[root@centos01 ~]# sed 's/the/THE/' test.txt
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
THE tongue is boneless but it breaks bones.12!
google is THE best tools for search keyword.
The year ahead will test our political establishment to THE limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
[root@centos01 ~]# sed 's/l/L/2' test.txt
he was short and fat.
He was wearing a blue poLo shirt with black pants.
The home of FootbalL on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tooLs for search keyword.
The year ahead wilL test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is reaLly spooky late at night!
Misfortunes never come alone/singLe.
I shouldn't have Lett so tast.
[root@centos01 ~]# sed 's/^/#/' test.txt
#he was short and fat.
#He was wearing a blue polo shirt with black pants.
#The home of Football on BBC Sport online.
#the tongue is boneless but it breaks bones.12!
#google is the best tools for search keyword.
#The year ahead will test our political establishment to the limit.
#PI=3.141592653589793238462643383249901429
#a wood cross!
#Actions speak louder than words
#
##woood #
#
##woooooood #
#
#
#AxyzxyzxyzxyzC
#
#
#I bet this place is really spooky late at night!
#Misfortunes never come alone/single.
#I shouldn't have lett so tast.
[root@centos01 ~]# sed '/the/s/o/0/g' test.txt
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the t0ngue is b0neless but it breaks b0nes.12!
g00gle is the best t00ls f0r search keyw0rd.
The year ahead will test 0ur p0litical establishment t0 the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
在Linux/UNIX系統(tǒng)中,awk是一個功能強大的編輯工具,逐行讀取輸入文本,并根據(jù)指定的匹配模式進行查找,對符合條件的內(nèi)容進行格式化輸出或者過濾處理,可以在無交互的情況下實現(xiàn)相當復雜的文本操作,被廣泛應用于Shell腳本,完成各種自動化配置任務。
通常情況下awk所使用的命令格式如下所示,其中,單引號加上大括號“{}”用于設置對數(shù)據(jù)進行的處理動作。awk可以直接處理目標文件也可以通過“-f”讀取腳本對目標文件進行處理。
awk 選項 '模式或條件 {編輯指令}' 文件1 文件2 ......
awk -f 腳本文件 文件1 文件2 ...
awk包含幾個特殊的內(nèi)建變量(可直接用)如下所示:
- NF:當前處理的行的字段個數(shù)。
- FS:指定每行文本的字段分隔符,默認為空格或制表位。
- NR:當前處理的行的字段個數(shù)。
- $0:當前處理的行的整行內(nèi)容。
- FILENAME:被處理的文件名。
- RS:數(shù)據(jù)記錄分隔,默認為\n,即每行為一條記錄。
[root@centos01 ~]# awk '{print}' test.txt
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
[root@centos01 ~]# awk 'NR==1,NR==3{print}' test.txt
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
[root@centos01 ~]# awk '(NR%2)==1{print}' test.txt
he was short and fat.
The home of Football on BBC Sport online.
google is the best tools for search keyword.
PI=3.141592653589793238462643383249901429
Actions speak louder than words
#woood #
#woooooood #
I bet this place is really spooky late at night!
I shouldn't have lett so tast.
[root@centos01 ~]# awk '(NR%2)==0{print}' test.txt
He was wearing a blue polo shirt with black pants.
the tongue is boneless but it breaks bones.12!
The year ahead will test our political establishment to the limit.
a wood cross!
AxyzxyzxyzxyzC
Misfortunes never come alone/single.
[root@centos01 ~]# awk '/^root/{print}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos01 ~]# awk '{print $1 $3}' test.txt
heshort
Hewearing
Theof
theis
googlethe
Theahead
PI=3.141592653589793238462643383249901429
across!
Actionslouder
#woood
#woooooood
AxyzxyzxyzxyzC
Ithis
Misfortunescome
Ihave
—————— 本文至此結束,感謝閱讀 ——————
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。