Chinaunix

标题: 问个sed初级问题,谢谢 [打印本页]

作者: sea_soft    时间: 2008-11-14 16:46
标题: 问个sed初级问题,谢谢
test-> cat quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

test-> sed -n '/The/,2'p quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
The local nurse Miss P.Neave was in attendance.

test-> sed -n '2,/The/'p quote.txt
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:00.
The local nurse Miss P.Neave was in attendance.

对sed -n '/The/,2'p quote.txt
和sed -n '2,/The/'p quote.txt
的含义不理解,这两种写法分别是什么含义,谢谢
作者: ILoveMK    时间: 2008-11-14 16:58
提示: 作者被禁止或删除 内容自动屏蔽
作者: sea_soft    时间: 2008-11-14 17:22
谢谢楼上的,但对于
sed -n '/The/,2'p quote.txt --匹配第一个含有The的行到第二行
-----------------------------
有些不理解,为什么显示的结果是:
test-> sed -n '/The/,2'p quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
The local nurse Miss P.Neave was in attendance.
而不是:
test-> sed -n '/The/,2'p quote.txt
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
呢,谢谢
作者: blackold    时间: 2008-11-18 11:15
标题: 回复 #3 sea_soft 的帖子
建议先看Sed基础。
作者: smallstar001    时间: 2008-11-18 11:28
原帖由 ILoveMK 于 2008-11-14 16:58 发表
这么简单的问题我来回答吧:
sed -n '/The/,2'p quote.txt --匹配第一个含有The的行到第二行

sed -n '2,/The/'p quote.txt --匹配第二行到下一个含有The的行


解释的不全对, 假设/The/匹配了1行和4行,

则这个/The/,2匹配的地址空间是

1,2
4
这两个

[ 本帖最后由 smallstar001 于 2008-11-18 11:30 编辑 ]
作者: waker    时间: 2008-11-18 11:31
标题: 回复 #3 sea_soft 的帖子
笼头打开不关水就会一直流到干
作者: ynchnluiti    时间: 2008-11-18 11:34
原帖由 waker 于 2008-11-18 11:31 发表
笼头打开不关水就会一直流到干

很形象啊。可能楼主过分在意第二行中的字了。
作者: 无声无息    时间: 2008-11-18 11:53
标题: 回复 #6 waker 的帖子
范围是闭合的,开水就要关水,做人有始有终
范围是包含开始的行,结束条件的行,以及两者之间的行。如果结束条件无法满足,就会操作到文件结尾,如果结束条件满足,就继续查找满足开始条件的位置.
作者: yezhulin1982    时间: 2008-11-18 13:11
学习了。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2