ITeye博客

sed 与正则表达式组合应用(一)

omygege · 2011-12-07 09:08 · Markdown

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.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。