sed 與正規表示式組合應用(一)

2021-08-26 11:52:54 字數 1596 閱讀 6700

sed 與正規表示式組合格式

sed 『/正規表示式/sed命令『

1、區分大小寫

$ echo "this is a test" |sed -n '/this/p'

$ echo "this is a test" |sed -n '/this/p'

this is a test

2、模式匹配的文字要大於或等於 表示式集合

$ echo "the books are expensive" | sed -n '/book/p'

the books are expensive

$ echo "the book are expensive" | sed -n '/books/p'

$ echo "the books are expensive" | sed -n '/book/p'

the books are expensive

$ echo "this is line number 1" | sed -n '/number 1/p'

this is line number 1

3、識別空格

$ cat >data1

this is a normal line of text.

this is a line with too many spaces.

$ sed -n '/ /p' data1

this is a line with too many spaces.

4、支援定位符^ $

$ echo "the book store" | sed -n '/^book/p'

$ echo "book store" | sed -n '/^book/p'

book store

$ echo "this ^ is a test" | sed -n '/s ^/p'

this ^ is a test

$ cat data4

this is a test of using both anchors

i said this is a test

this is a test

i'm sure this is a test

$ sed -n '/^$/d' data4

$ sed '/^$/d' data4

this is a test of using both anchors

i said this is a test

this is a test

i'm sure this is a test

5、支援. 字元代替任何字元

$ cat >data6

this is a test of a line

the cat is sleeping.

that is a very nice cat.

this test is at line four.

at ten o'clock we'll go home.

$ sed -n '/.at/p' data6

the cat is sleeping.

that is a very nice cat.

this test is at line four.

sed 與正規表示式組合應用(一)

sed 與正規表示式組合格式 sed 正規表示式 sed命令 1 區分大小寫 echo this is a test sed n this p echo this is a test sed n this p this is a test 2 模式匹配的文字要大於或等於 表示式集合 echo the...

sed 正規表示式

如果testfile的內容是 welcome to the world of regexp 現在要去掉所有的html標籤,使輸出結果為 hello world welcome to the world of regexp 怎麼做呢?如果用下面的命令 sed s g testfile 結果是兩個空行,...

sed 常用正規表示式

1.乙個比較實用的正規表示式 匹配html的嵌入 匹配 的嵌入碼 刪除僅由空字元組成的行 sed space d filename 匹配html標籤 例如 從html檔案中剔除html標籤 sed s g space d file.html 例如 要從下列 中去除 及其中包括的 b 4c6c2a65...