濮阳杆衣贸易有限公司

主頁 > 知識庫 > shell高級學習之正則表達式

shell高級學習之正則表達式

熱門標簽:湖北孝感如何辦理 外呼系統的合法性 威海電銷外呼系統好用嗎 地圖標注x是啥意思 同花順電話機器人微信 北京電銷機器人對市場的影響 武漢語音電銷機器人加盟 房產證地圖標注的兩個面積 輝縣市地圖標注

正則表達式概述

正則表達式是一種定義的規(guī)則,Linux工具可以用它來過濾文本。

基礎正則表達式

純文本

[root@node1 ~]# echo "this is a cat" | sed -n '/cat/p'
this is a cat
[root@node1 ~]# echo "this is a cat" | gawk '/cat/{print $0}'
this is a cat

 正則表達式的匹配非常挑剔,尤其需要記住,正則表達式區(qū)分大小寫。

特殊字符

正則表達式識別的特殊字符包括:

.*[]^${}\+?|()

如果要使用某個特殊字符作為文本字符,就必須轉義,一般用(\)來轉義。

[root@node1 ~]# echo "this is a $" | sed -n '/\$/p'
this is a $

錨字符

有兩個特殊字符可以用來將模式鎖定在數據流的行首或行尾

脫字符(^)定義從數據流中文本行的行首開始的模式。

美元符($)定義了行尾錨點。

[root@node1 ~]# echo "this is a cat" | sed -n '/^this/p'
this is a cat
[root@node1 ~]# echo "this is a cat" | sed -n '/cat$/p'
this is a cat

在一些情況下可以組合使用這兩個命令

1.比如查找只含有特定文本的行

[root@node1 ljy]# more test.txt  
this is a dog
what
how
this is a cat
is a dog
[root@node1 ljy]# sed -n '/^is a dog$/p' test.txt
is a dog
[root@node

2.兩個錨點組合起來,可以直接過濾空白行

[root@node1 ljy]# more test.txt  
this is a dog
what
how
 
this is a cat
is a dog
[root@node1 ljy]# sed '/^$/d' test.txt 
this is a dog
what
how
this is a cat
is a dog

點號字符

點號用來匹配除換行符外的任意單個字符,他必須匹配一個字符。

[root@node1 ljy]# more test.txt
this is a dog
what
how
this is a cat
is a dog
at
[root@node1 ljy]# sed -n '/.at/p' test.txt
what
this is a cat

字符組

限定待匹配的具體字符,使用字符組。使用方括號來定義一個字符組。

[root@node1 ljy]# more test.txt
this is a dog
this is a Dog
this is a DoG
this is a cat
[root@node1 ljy]# sed -n '/[dD]og/p' test.txt
this is a dog
this is a Dog
[root@node1 ljy]# sed -n '/[dD]o[gG]/p' test.txt 
this is a dog
this is a Dog
this is a DoG

排除型字符組

要排除某些特定的元素,要在字符組前面加個脫字符。

[root@node1 ljy]# sed -n '/[dD]o[gG]/p' test.txt 
this is a dog
this is a Dog
this is a DoG
[root@node1 ljy]# sed -n '/[^D]og/p' test.txt 
this is a dog

區(qū)間

正則表達式會包括此區(qū)間內的任意字符。

[root@node1 ljy]# more test.txt
123123
1231
121222222
412345341613
vsdvs
qwer12344123
12345
34211
444444
[root@node1 ljy]# sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' test.txt
12345
34211

拓展正則表達式

問號

問號表明前面的字符出現0次或者1次,僅限于此。

[root@node1 ljy]# echo "bat" | gawk '/ba?t/{print $0}' 
bat
[root@node1 ljy]# echo "baat" | gawk '/ba?t/{print $0}'
[root@node1 ljy]# echo "bt" | gawk '/ba?t/{print $0}' 
bt

可以將問號和字符組一起使用

[root@node1 ljy]# echo "bt" | gawk '/b[ae]?t/{print $0}'
bt
[root@node1 ljy]# echo "bat" | gawk '/b[ae]?t/{print $0}'
bat
[root@node1 ljy]# echo "bet" | gawk '/b[ae]?t/{print $0}'
bet
[root@node1 ljy]# echo "baat" | gawk '/b[ae]?t/{print $0}'

加號

加號表明前面的字符可以出現一次或多次,但至少是1次。

[root@node1 ljy]# echo "baat" | gawk '/b[ae]+t/{print $0}'
baat
[root@node1 ljy]# echo "bt" | gawk '/b[ae]+t/{print $0}' 
[root@node1 ljy]# echo "bt" | gawk '/ba+t/{print $0}' 
[root@node1 ljy]# echo "bat" | gawk '/ba+t/{print $0}'
bat
[root@node1 ljy]# echo "baat" | gawk '/ba+t/{print $0}'
baat

花括號

ERE中的花括號允許你為可重復的正則表達式規(guī)定上下限。

m,n最少出現m此,最多出現n次。

[root@node1 ljy]# echo "baat" | gawk '/b[ae]{1,2}t/{print $0}' 
baat
[root@node1 ljy]# echo "baaat" | gawk '/b[ae]{1,2}t/{print $0}'

管道符號

用邏輯or的方式指定正則表達式規(guī)則,其中一個條件符合要就即可。

表達式分組

正則表達式分組也可以用圓括號進行分組。

[root@node1 ljy]# echo "bat" | gawk '/b(a|e)t/{print $0}'  
bat
[root@node1 ljy]# echo "baat" | gawk '/b(a|e)t/{print $0}'
[root@node1 ljy]# echo "bet" | gawk '/b(a|e)t/{print $0}' 
bet

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • shell 正則表達式詳細整理
  • Shell正則表達式之grep、sed、awk實操筆記
  • Shell if中的正則表達式使用詳解
  • Shell腳本中通過正則表達式匹配IP地址
  • Shell正則表達式驗證IP地址
  • PowerShell中正則表達式使用例子
  • linux shell 路徑截取正則表達式
  • Shell正則表達式學習筆記
  • PowerShell中使用正則表達式匹配字符串實例
  • 詳解Linux--shell腳本之正則表達式

標簽:武威 蚌埠 日喀則 麗江 迪慶 安康 西寧 紹興

巨人網絡通訊聲明:本文標題《shell高級學習之正則表達式》,本文關鍵詞  shell,高級,學,習之,正則,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《shell高級學習之正則表達式》相關的同類信息!
  • 本頁收集關于shell高級學習之正則表達式的相關信息資訊供網民參考!
  • 推薦文章
    兴业县| 刚察县| 吉木萨尔县| 石门县| 梨树县| 怀远县| 光山县| 宁津县| 紫云| 米脂县| 宣汉县| 宜兴市| 同仁县| 四川省| 多伦县| 习水县| 漾濞| 肇州县| 垦利县| 富宁县| 台中市| 宁晋县| 抚远县| 柞水县| 赤水市| 水城县| 威信县| 澳门| 锡林浩特市| 红河县| 德化县| 正镶白旗| 彩票| 容城县| 东台市| 沈丘县| 廉江市| 邵阳县| 宁武县| 隆林| 阿克苏市|