---
title: "sed 与正则表达式组合应用（一）"
author: "omygege"
date: "2011-12-07T09:08:00Z"
tags: []
slug: "sed"
draft: false
lastmod: "2011-12-08T17:20:21Z"
id: 1298878
url: "https://omygege.iteye.com/blog/1298878"
---

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 &gt;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 &gt;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.