sed與正則表達式

本文轉載自:https://www.cnblogs.com/zd520pyx1314/p/6061339.html 作者:zd520pyx1314 轉載請註明該聲明。

行的開頭(^)

^匹配每一行的開頭

[root@sishen ~]# sed -n '/^103/ p ' employee.txt

103,Raj Reddy,Sysadmin

只有^出現在正則表達式開頭時,它才匹配行的開頭,所以,^N匹配所有以N開頭的行。

行的結尾($)

$匹配行的結尾

顯示以字符r結尾的行

[root@sishen ~]# sed -n '/r$/ p' employee.txt

102,Jason Smith,IT Manager

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

單個字符(.)

元字符點 . 匹配除換行符之外的任意單個字符

l . 匹配單個字符

l . . 匹配兩個字符

l . . .匹配三個字符

l …..依次類推

下面的例子中,模式”J後面跟三個字符和一個空格”將被替換爲“Jason後面一個空格”

所以“J····“後面同時匹配employee.txt文件中的“John”和“Jane”,替換結果如下

[root@sishen ~]# sed -n 's/J.../Jason/ p' employee.txt

101,Jason Doe,CEO

102,Jasonn Smith,IT Manager

105,Jason Miller,Sales Manager

匹配0次或多次(*)

星號*匹配0個或多個其前面的字符,如:1*匹配0個或多個1

首先建立測試文件

[root@sishen ~]# vim log.txt

log:input.txt

log:

log:testing resumed

log:

log:output created

例如:只查看包含log且後面有信息的行

[root@sishen ~]# sed -n '/log: *./p' log.txt #注意log冒號後面有空格,而且點也是必須的

log:input.txt

log:testing resumed

log:output created

匹配一次或多次(\+)

“\+”匹配一次或多次他前面的字符,例如 空格\+ 或 “\+“匹配至少一個或多個空格

只顯示包含log且log後面有一個或多個空格的所有行

[root@sishen ~]# sed -n '/log: \+/ p' log.txt

log: input.txt

log: testing resumed

注意<tab>與空格不同

零次或一次匹配(\?)

\?匹配0次貨一次他前面的字符

[root@sishen ~]# sed -n '/log: \?/ p' log.txt

log: input.txt

log:

log: testing resumed

log:

log:output created

轉義字符(\)

如果要在正則表達式中搜尋特殊字符(如:*, .)必須使用\來轉義它們

[root@sishen ~]# sed -n '/127\.0\.0\.1/ p' /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

字符集[0-9]

匹配包含2、3、或者4的行

[root@sishen ~]# sed -n '/[234]/p' employee.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

注意:方括號中,可以是喲個連接字符指定一個字符範圍,如[0123456789]可以用[0-9]表示,字母可以用[a-z],[A-Z]表示,等等

匹配包含2、3或者4的行另一種方式

[root@sishen ~]# sed -n '/[2-4]/p' employee.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

其他正則表達式

或操作符(|)

管道符號用來匹配兩邊任意一個子表達式,子表達式1|子表達式2匹配子表達式1或者子表達式2

打印包含101或者包含102的行

[root@sishen ~]# sed -n '/101\|102/p' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

打印包含數字2~3或者包含105的行

[root@sishen ~]# sed -n '/[2-3]\|105/p' employee.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

105,Jane Miller,Sales Manager

精確匹配m次({m})

正則表達式後面跟上{m}註明精確匹配該正則m次

首先建立測試文件

[root@sishen ~]# vim number.txt

1

12

123

1234

12345

123456

打印包含任意數字的行(相當於打印所有的行對於該文件來說)

[root@sishen ~]# sed -n '/[0-9]/p' number.txt

1

12

123

1234

12345

123456

打印只包含5個數字的行

[root@sishen ~]# sed -n '/^[0-9]\{5\}$/p' number.txt

12345

注意這裏一定要有開頭和結尾符號

匹配m至n次({m,n})

正則表達式後面跟上{m,n}表明精確匹配該正則至少m,最多n次,m和n不能是負數,並且要小於255

打印由3至5個數字組成的行

[root@sishen ~]# sed -n '/^[0-9]\{3,5\}$/ p' number.txt

123

1234

12345

正則表達式後面跟上{m,}表明精確匹配該正則至少m,最多不限,(同樣如果是{,n}表明最多n次最少一次)

字符邊界(\b)

\b用來匹配單詞開頭(\bxx)或(xx\b)的任意字符,因此\bthe\b將匹配the,但不匹配they,\bthe將匹配the或they

首先建立測試文件

[root@sishen ~]# vim words.txt

word matching using:the

word matching using:thethe

word matching using:they

匹配包含the作爲整個單詞的行

[root@sishen ~]# sed -n '/\bthe\b/ p' words.txt

word matching using:the

注意如果沒有後面的那個\b,效果將等同匹配包含所有以the開頭的單詞的行

[root@sishen ~]# sed -n '/\bthe/ p' words.txt

word matching using:the

word matching using:thethe

word matching using:they

回溯引用(\n)

使用回溯引用可以給正則表達式分組,以便後面引用他們

只匹配重複the兩次的行

[root@sishen ~]# sed -n '/\(the\)\1/ p' words.txt

word matching using:thethe

同理,“\([0-9]\)\1“匹配連續兩個相同的數字,如11,22,33 ····

在sed替換中使用正則表達式

把employee.txt 中每行最後兩個字符替換爲“Not defined “:

[root@sishen ~]# sed -n 's/..$/,Not Defined/ p' employee.txt

101,John Doe,C,Not Defined

102,Jason Smith,IT Manag,Not Defined

103,Raj Reddy,Sysadm,Not Defined

104,Anand Ram,Develop,Not Defined

105,Jane Miller,Sales Manag,Not Defined

刪除Manager及其以後的字符:

[root@sishen ~]# sed 's/Manager.*//' employee.txt | cat -A

101,John Doe,CEO$

102,Jason Smith,IT $

103,Raj Reddy,Sysadmin$

104,Anand Ram,Developer$

105,Jane Miller,Sales $

注意:原文中沒有“|cat -A“,是爲了表現102和105最後的空格而添加的

刪除所有以#開頭的行

[root@sishen ~]# cat employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

#106,Jane Miller,Sales Manager

#107,Jane Miller,Sales Manager

[root@sishen ~]# sed -e 's/^#.*// ; /^$/d' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@sishen ~]# sed '/^#/d' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

首先建立test.html文件

[root@sishen ~]# vim test.html

<html><body><h1>Hello word!</h1></body></html>

清楚test.html文件中的所有html標籤

[root@sishen ~]# sed 's/<[^>]*>//g' test.html

Hello word!

刪除所有註釋和空行

[root@sishen ~]# sed -e 's/#.*//;/^$/ d' /etc/profile

只刪除註釋行不刪除空行

[root@sishen ~]# sed '/^#.*/d' /etc/profile

使用sed可以把DOS的換行符(CR/LF)替換爲Unix格式。當把DOS格式的文件拷貝到Unix上,你會發現,每行結尾都有\r\n

使用sed把DOS格式的文件轉換爲Unix格式

[root@sishen ~]# sed 's/.$//' filename

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