遇到一段奇怪的sed代码
发表于 : 2012-01-08 14:01
有一个文本如下:
$ cat expl.6
I want to see @fl(what will happen) if we put the
font change commands @fl(on a set of lines). If I understand
things (correctly), the @fl(third) line causes problems. (No?).
Is this really the case, or is it (maybe) just something else?
Let's test having two on a line @fl(here) and @fl(there) as
well as one that begins on one line and ends @fl(somewhere
on another line). What if @fl(it is here) on the line?
Another @fl(one).
现在要作的就是将"fl@(…)替换为"\fB(…)\fR。以下就是满足条件的sed命令:
$ sed 's/@fl(\([^)]*\))/\\fB\1\\fR/g
> /@fl(.*/{
> N
> s/@fl(\(.*\n[^)]*\))/\\fB\1\\fR/g
> P
> D
> }' expl.6
然而,如果不使用这种输入输出循环,而是单单用N来实现的话,就会出现问题:
$ sed 's/@fl(\([^)]*\))/\\fB\1\\fR/g
> /@fl(.*/{
> N
> s/@fl(\(.*\n[^)]*\))/\\fB\1\\fR/g
> }' expl.6
我运行了两段代码,发现第二段代码的问题出在文本倒数第二行的@fl(it is here)没有被替换。文本前六行都没有触发程序第二行。到第七行时触发程序第二行,于是模式空间追加读入文本第八行,进行程序第四行命令的替换,g选项应该会将两个@fl都替换掉,但最后却没有,why?和第一段代码相比,第二段代码只是删去了P和D,加入P和D两个命令怎么就能全部替换?
百思不得其解,劳驾各位大侠指个路。谢谢!
$ cat expl.6
I want to see @fl(what will happen) if we put the
font change commands @fl(on a set of lines). If I understand
things (correctly), the @fl(third) line causes problems. (No?).
Is this really the case, or is it (maybe) just something else?
Let's test having two on a line @fl(here) and @fl(there) as
well as one that begins on one line and ends @fl(somewhere
on another line). What if @fl(it is here) on the line?
Another @fl(one).
现在要作的就是将"fl@(…)替换为"\fB(…)\fR。以下就是满足条件的sed命令:
$ sed 's/@fl(\([^)]*\))/\\fB\1\\fR/g
> /@fl(.*/{
> N
> s/@fl(\(.*\n[^)]*\))/\\fB\1\\fR/g
> P
> D
> }' expl.6
然而,如果不使用这种输入输出循环,而是单单用N来实现的话,就会出现问题:
$ sed 's/@fl(\([^)]*\))/\\fB\1\\fR/g
> /@fl(.*/{
> N
> s/@fl(\(.*\n[^)]*\))/\\fB\1\\fR/g
> }' expl.6
我运行了两段代码,发现第二段代码的问题出在文本倒数第二行的@fl(it is here)没有被替换。文本前六行都没有触发程序第二行。到第七行时触发程序第二行,于是模式空间追加读入文本第八行,进行程序第四行命令的替换,g选项应该会将两个@fl都替换掉,但最后却没有,why?和第一段代码相比,第二段代码只是删去了P和D,加入P和D两个命令怎么就能全部替换?
百思不得其解,劳驾各位大侠指个路。谢谢!