sed中的“保留空间"和“模式空间"的概念 [英] The Concept of 'Hold space' and 'Pattern space' in sed

查看:183
本文介绍了sed中的“保留空间"和“模式空间"的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对sed中的两个概念感到困惑:保持空间和模式空间.有人可以帮忙解释一下吗?

I'm confused by the two concepts in sed: hold space and pattern space. Can someone help explain them?

以下是手册的摘要:

h H    Copy/append pattern space to hold space.
g G    Copy/append hold space to pattern space.

n N    Read/append the next line of input into the pattern space.

这六个命令确实让我感到困惑.

These six commands really confuse me.

推荐答案

当sed逐行读取文件时,当前已读取的行将插入到 pattern 缓冲区(模式空间)中.模式缓冲区就像临时缓冲区一样,即存储当前信息的暂存器.当您告诉sed打印时,它将打印图案缓冲区.

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.

保持缓冲区/保持空间就像一个长期存储,这样您就可以在sed处理另一行时捕获,存储并在以后重用它.您不直接处理保留空间,相反,如果您想对其进行处理,则需要将其复制或追加到模式空间.例如,打印命令p仅打印图案空间.同样,s在模式空间上操作.

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.

这里是一个例子:

sed -n '1!G;h;$p'

(-n选项禁止自动打印行)

(the -n option suppresses automatic printing of lines)

此处有三个命令:1!Gh$p. 1!G的地址为1(第一行),但是!表示该命令将在第一行的 but 处执行.另一方面,$p将仅在最后一行执行.那么会发生什么:

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:

  1. 读取第一行并将其自动插入模式空间
  2. 在第一行上,不执行第一条命令; h将第一行复制到保留空间.
  3. 现在第二行替换模式空间中的所有内容
  4. 在第二行上,首先我们执行G,将保持缓冲区的内容附加到模式缓冲区,并用换行符将其分隔.模式空间现在包含第二行,换行和第一行.
  5. 然后,h命令将模式缓冲区的并置内容插入到保留空间中,该空间现在保留反转的行2和1.
  6. 我们继续进行第三行-转到上面的点(3).
  1. first line is read and inserted automatically into the pattern space
  2. on the first line, first command is not executed; h copies the first line into the hold space.
  3. now the second line replaces whatever was in the pattern space
  4. 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.
  5. Then, h command inserts the concatenated contents of the pattern buffer into the hold space, which now holds the reversed lines two and one.
  6. We proceed to line number three -- go to the point (3) above.

最后,在读取最后一行并将保持空间(以相反的顺序包含所有先前的行)添加到图案空间之后,图案空间将用p打印.您已经猜到了,上面的命令确实执行了tac命令的作用-反向打印文件.

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, pattern space is printed with p. As you have guessed, the above does exactly what the tac command does -- prints the file in reverse.

这篇关于sed中的“保留空间"和“模式空间"的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆