Copied from: stackoverflow

When sed reads a file line by line, the line that has been currently read is inserted into the pattern buffer (pattern space). Pattern buffer is like the temporary buffer, the scratchpad where the current information is stored. When you tell sed to print, it prints the pattern buffer.

Hold buffer / hold space is like a long-term storage, such that you can catch something, store it and reuse it later when sed is processing another line. You do not directly process the hold space, instead, you need to copy it or append to the pattern space if you want to do something with it. For example, the print command p prints the pattern space only. Likewise, s operates on the pattern space.

Here is an example:
sed -n ‘1!G;h;$p’

(the -n option suppresses automatic printing of lines)

There are three commands here: 1!G, h and $p. 1!G has an address, 1 (first line), but the ! means that the command will be executed everywhere but on the first line. $p on the other hand will only be executed on the last line. So what happens is this:

first line is read and inserted automatically into the pattern space
on the first line, first command is not executed; h copies the first line into the hold space.
now the second line replaces whatever was in the pattern space
on the second line, first we execute G, appending the contents of the hold buffer to the pattern buffer, separating it by a newline. The pattern space now contains the second line, a newline, and the first line.
Then, h command inserts the concatenated contents of the pattern buffer into the hold space, which now holds the reversed lines two and one.
We proceed to line number three – go to the point (3) above.

Finally, after the last line has been read and the hold space (containing all the previous lines in a reverse order) have been appended to the pattern space, p 大专栏  pattern space and hold space of sedattern space is printed with p. As you have guessed, the above does exactly what the tac command does – prints the file in reverse.

—- translation —-
当 sed 从文件中一行一行的读取时,当前被读取的那一行会被插入到 pattern buffer(pattern space)。pattern buffer 就像是一个临时性的缓冲区,中间结果寄存器就是把当前的信息先暂存到里面。当你通知 sed 去打印的时候,它打印的就是 pattern buffer。

Hold buffer / hold space 更像是一个长期的寄存器,你可以捕获信息,存储并且过后再重用它们当 sed 在处理另一行的时候。你不可能直接操作 hold space,然而,如果你想要用它干点什么的时候可以拷贝它并把它追加到 pattern space 。举例来说,打印命令 p 只是将 pattern space 打印出来。相同的,s 命令只是在 pattern space 之上进行操作。

咱们来实际操作一下:
sed -n ‘1!G;h;$p’

(-n 选项就是取消了自动打印)

这里有三个命令:1!G, h and $p. 1!G 有一个 address,1(第一行),但是 ! 意味着命令可以在任何地方执行出了第一行。$p 与之不同只会在最后一行执行。实际过程是这样的:

第一行被读取并自动插入到了 pattern space
在第一行,第一条命令并没有被执行;h 将第一行拷贝到 hold space。
现在第二行取代了 pattern space 中的内容
在第二行,我们首先执行 G,追加 hold buffer 中的内容到 pattern buffer,用一个换行符将其分割。pattern space 中现在包含第二行,一个换行符,还有第一行。
接下来,h 命令将已经连接在一起的 pattern buffer 插入到 hold space,那里现在保存的是倒序的第二行和第一行。
之后我们继续处理第三行 -- 重复上面的第三步。

最终,在最后一行被已经读取并且 hold space(包含着所有倒序的行)已经被追加到了 pattern space 之后,pattern space 被 p 打印出来。正如你所预料的,以上正是 tac 命令所做的事情 – 倒序打印文件。

pattern space and hold space of sed的更多相关文章

  1. 关于sed中的Pattern Space和Hold Space的随笔

    首先是一部分概念和示例,这部分转自:http://coolshell.cn/articles/9104.html Pattern Space 第零个是关于-n参数的,大家也许没看懂,没关系,我们来看一 ...

  2. sed高级用法:模式空间(pattern space)和保持空间(hold space)

    摘自:https://blog.csdn.net/ITsenlin/article/details/21129405 sed高级用法:模式空间(pattern space)和保持空间(hold spa ...

  3. User space 与 Kernel space

    学习 Linux 时,经常可以看到两个词:User space(用户空间)和 Kernel space(内核空间). 简单说,Kernel space 是 Linux 内核的运行空间,User spa ...

  4. JVM内存区域划分Eden Space、Survivor Space、Tenured Gen,Perm Gen解释

    以下内容转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=29632145&id=4616836 jvm区域总体分两 ...

  5. JVM内存区域划分Eden Space、Survivor Space、Tenured Gen,Perm Gen解释(转)

    jvm区域总体分两类,heap区和非heap区.heap区又分:Eden Space(伊甸园).Survivor Space(幸存者区).Tenured Gen(老年代-养老区). 非heap区又分: ...

  6. JVM内存区域划分Eden Space,Survivor Space,Tenured Gen,Perm Gen

    jvm区域总体分两类,heap区和非heap区.heap区又分:Eden Space(伊甸园).Survivor Space(幸存者区).Tenured Gen(老年代-养老区). 非heap区又分: ...

  7. JVM内存区域详解(Eden Space、Survivor Space、Old Gen、Code Cache和Perm Gen)

    JVM区域总体分两类,heap区和非heap区.heap区又分为: Eden Space(伊甸园). Survivor Space(幸存者区). Old Gen(老年代). 非heap区又分: Cod ...

  8. Permanent Space 和 Heap Space

      JVM堆内存 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...

  9. 法线从object space到eye space的转换((normal matrix)

    对于顶点来说,从object Space转换到eye space, 使用model-view矩阵就好了.那么顶点的法线是否也可以直接使用model-view矩阵转化? 通常情况下是不行的. 如下两张图 ...

随机推荐

  1. BBS配置

    BBS配置 一.url路由 """BBS URL Configuration The `urlpatterns` list routes URLs to views. F ...

  2. 关于RL78 系列的bootloader

    1.充分了解芯片FLASH结构分布,对FLASH进行分区 2.熟练使用FSL库 3.调试中断映射功能 4.调试一种通信方式,UART,CAN等 5.对FLASH进行编程,执行跳转APP程序,调试一个多 ...

  3. mac词典安装

    网上下载词典文件,比如https://www.xiaomac.com/2013121204.html,将朗道词典解压放入这里. $ ls /Library/Dictionaries/ langdao- ...

  4. 【Mongodb】mongoDB与mongoose---Scheme和Collections对应问题

    mongodb是一个基于分布式文件存储的文档型数据库 MongoDB 是一个介于关系数据库和非关系数据库之间的产品 MongoDB 最大的特点是他支持的查询语言非常强大,而且还支持对数据建立索引 官方 ...

  5. [GX/GZOI2019]宝牌一大堆(DP)

    出这种麻将题有意思吗? 乍看很难实则很水,就是麻将式DP,想必大家很熟悉了吧.首先把“国士无双”和“七对子”两种牌型判掉,然后观察牌胡的形式,发现每多一张牌实际上就是把1个面子变成1个杠子,然后可以直 ...

  6. set_include_path详细解释(转)

    首先我们来看这个全局变量:__FILE__它表示文件的完整路径(当然包括文件名在内)也就是说它根据你文件所在的目录不同,有着不同的值:当然,当它用在包行文件中的时候,它的值是包含的路径: 然后:我们看 ...

  7. 引力波的绘制(python)

    import numpy as np import matplotlib.pyplot as plt from scipy.io import wavfile rate_h,hstrain = wav ...

  8. P2448 无尽的生命(树状数组+离散化)

    题目描述 逝者如斯夫,不舍昼夜! 叶良辰认为,他的寿命是无限长的,而且每天都会进步. 叶良辰的生命的第一天,他有1点能力值.第二天,有2点.第n天,就有n点.也就是S[i]=i 但是调皮的小A使用时光 ...

  9. Django实现注册,往邮箱发送验证链接

    由于最近要做个平台,在GitHub上下载了一个系统框架,想着为了安全,实现注册时往一个邮箱发送注册信息,由管理员来确认是否同意其注册. 感谢博主:https://blog.csdn.net/geek_ ...

  10. IntelliJ IDEA项目断开版本管理解决方案

    今天使用idea时打开项目突然发现项目不受svn管理(项目目录依然受svn管理,只是idea脱管了),如遇到可用以下方法: 图片示例: 1. 2. 希望能帮到你