- 论坛徽章:
- 0
|
test:
line one
line two
line three
sed '1!G;h;$!d' test
//此处为引用
第一步,sed扫描到第一行,直接执行第二个命令,将模式空间中的内容拷贝到暂存空间中,此时模式空间中是line one,暂存空间中是line one,然后执行第三个命令,删除了模式空间中的第一行,此时模式空间中为空,暂存空间中为line one,
第二步,sed扫描到了第二行,会执行第一个命令G了,此时模式空间中是line two,暂存空间中是line one,G将换行符和暂存空间内容追加到模式空间中,此时模式空间是line two\nline one,暂存空间是line one,然后执行第二个命令,将模式空间中的内容拷贝到暂存空间中,此时模式空间不变,暂存空间为line two\nline one,执行第三个命令之后,模式空间为空,暂存空间为line two\nline one
第三步,sed扫描到第三行,会执行第一个命令G,模式空间为line three,暂存空间是line two\nline one,执行之后模式空间是line three\nline two\nline one,暂存空间是line two\nline one,然后h命令,模式空间不变,暂存空间line three\nline two\nline one然后不执行第三条命令,ok,结束,打印的就是最后模式空间爱你中的内容line three\nline two\nline one
输出结果:
line three
line two
line one
=============================================
问题: 为什么最后一次进行$!d 没有运行
如果最后一次$!d运行生效 最后输出结果不是应该只有 line one 了么
|
|