国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

shell高級學習之正則表達式

2019-10-26 18:59:58
字體:
來源:轉載
供稿:網友

正則表達式概述

正則表達式是一種定義的規則,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

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

特殊字符

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

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

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

[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 dogwhathowthis is a catis a dog[root@node1 ljy]# sed -n '/^is a dog$/p' test.txtis a dog[root@node

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

[root@node1 ljy]# more test.txt  this is a dogwhathow this is a catis a dog[root@node1 ljy]# sed '/^$/d' test.txt this is a dogwhathowthis is a catis a dog

點號字符

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

[root@node1 ljy]# more test.txtthis is a dogwhathowthis is a catis a dogat[root@node1 ljy]# sed -n '/.at/p' test.txtwhatthis is a cat

字符組

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

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

排除型字符組

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

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

區間

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

[root@node1 ljy]# more test.txt1231231231121222222412345341613vsdvsqwer123441231234534211444444[root@node1 ljy]# sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' test.txt1234534211            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石楼县| 大姚县| 乐平市| 卢湾区| 慈利县| 乌鲁木齐县| 夏邑县| 北川| 土默特左旗| 通海县| 长春市| 富蕴县| 宜川县| 大关县| 南汇区| 左云县| 宜宾市| 随州市| 吉林省| 滁州市| 古蔺县| 双辽市| 井冈山市| 合水县| 延寿县| 阳东县| 九龙城区| 左云县| 江达县| 普洱| 施甸县| 昭平县| 京山县| 将乐县| 鲁甸县| 民丰县| 库伦旗| 罗江县| 正阳县| 宜川县| 永兴县|