sed ‘p;n’ – 无法找到这个sed命令背后的任何逻辑。

huangapple go评论55阅读模式
英文:

sed 'p;n' – Can't find any logic behind this sed command

问题

案例1:

% seq 4 | sed 'p'
1
1
2
2
3
3
4
4

由于未指定-n选项,_sed_默认会打印每一行,然后因为p命令的存在而再次打印。这一点,我理解得很好。

案例2:

% seq 4 | sed -n 'p'
1
2
3
4

这个案例对我来说也非常合理。默认情况下不打印,除非明确使用p命令进行指定。因此,不会出现重复行,如预期的那样。

案例3:

% seq 4 | sed 'p;n'
1
1
2
3
3
4

因为没有使用-n选项,我理解_sed_默认会打印所有行。然后,p命令会再次打印每一行。接下来,n命令会再次打印模式空间,清除它并重新加载下一行。因此,如果我关注的是第一行,我应该看到1被打印了三次,而不是两次。

我想了解_sed_在这种特殊情况下的工作方式。

英文:

Case 1:

% seq 4 | sed 'p'
1
1
2
2
3
3
4
4

Because -n is not stated, sed prints every line by default, and reprints again because of the p command. This, I understand well.

Case 2:

% seq 4 | sed -n 'p'
1
2
3
4

This one seems very logical to me too. Do not print by default, unless explicitly stated by p command. So, no duplicates, as expected.

Case 3:

% seq 4 | sed 'p;n'
1
1
2
3
3
4

Because there's no -n option, I understand sed should print all lines by default. Later, p command should reprint every line. Then, n command reprints out again the pattern space, clears it and reloads next line. So, if my focus is on line number 1, I should see 1 printed three times, instead of twice.

I'd like to know how sed works for this particular case.

答案1

得分: 2

sed应默认打印所有行。稍后,p命令

这一行不会在输入时打印。它会在脚本的末尾打印,就在读取下一行之前。sed 'p;n <here>'

Sed的工作方式如下:

  • 首先,第一行加载到模式空间中。
  • 然后,p打印模式空间。
  • 然后,n
    • 打印_当前_模式空间(除非使用了-n选项)
    • 加载下一行(第二行)到模式空间中。
  • 然后脚本结束,所以:
    • 模式空间被打印(除非使用了-n选项)
    • 然后加载下一个(第三个)行
    • 脚本重新开始。

所以在第一个示例中:

sed默认打印每一行,并且因为p命令而重新打印

不,首先p打印模式空间,然后sed会自动重新打印模式空间并加载下一行。"re-"打印是在脚本的末尾完成的,而不是由p命令完成的。

让我们参考一些文档 https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html 强调部分:

-n

抑制默认输出(即每行在编辑后都写入标准输出)。[...]

[...] 如果没有命令明确开始新的循环,那么在脚本结束时,模式空间将被复制到标准输出(除非指定了-n),并删除模式空间。[...]

英文:

> sed should print all lines by default. Later, p command

The line is not printed on input. It is printed on the end of the script, right before reading the next line. sed &#39;p;n &lt;here&gt;&#39;

Sed works like this:

  • First line is loaded in pattern space.
  • Then p prints the pattern space.
  • Then n:
    • Prints the current pattern space (unless -n)
    • Loads the next (second) line in pattern space.
  • Then script ends, so:
    • Pattern space is printed (unless -n)
    • Then the next (third) line is loaded
    • and the script starts again.

So in the first example:

> sed prints every line by default, and reprints again because of the p command

No, first p prints the pattern space, then sed automatically reprints the pattern space and loads the next line. The "re-"printing is done on the end of the script, not by p command.

Let's throw some reference https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html emphasis mine:

> -n
>
> Suppress the default output (in which each line, after it is examined for editing, is written to standard output). [...]
>
>
> [...] If no commands explicitly started a new cycle, then at the end of the script the pattern space shall be copied to standard output (except when -n is specified) and the pattern space shall be deleted. [...]

huangapple
  • 本文由 发表于 2023年6月13日 00:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定