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

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

问题描述

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

这是手册的片段:

<块引用>

h H 复制/追加模式空间以保存空间.g G 将保留空间复制/追加到模式空间.n N 将输入的下一行读取/追加到模式空间中.

这六个命令真的让我很困惑.

解决方案

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

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

这是一个例子:

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

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

这里有三个命令:1!Gh$p.1!G 有一个地址,1(第一行),但是 ! 意味着命令会在任何地方执行but 在第一行.$p 另一方面只会在最后一行执行.那么会发生什么:

  1. 第一行被读取并自动插入模式空间
  2. 第一行,第一条命令没有执行;h 将第一行复制到 hold 空间.
  3. 现在第二行替换了模式空间中的任何内容
  4. 在第二行,首先我们执行G,将保持缓冲区的内容附加到模式缓冲区,并用换行符分隔.模式空间现在包含第二行、换行符和第一行.
  5. 然后,h 命令将模式缓冲区的连接内容插入到保持空间中,该空间现在保存反转的第二行和第一行.
  6. 我们继续第 3 行 - 转到上面的第 (3) 点.

最后,在读完最后一行并将保持空间(以相反顺序包含所有前面的行)附加到模式空间后,模式空间用 p 打印.正如您已经猜到的,上面的代码与 tac 命令所做的完全一样——反向打印文件.

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

Here's a snippet of the manual:

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.

解决方案

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:

  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.

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天全站免登陆