英文:
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 'p;n <here>'
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.
- Prints the current pattern space (unless
- Then script ends, so:
- Pattern space is printed (unless
-n
) - Then the next (third) line is loaded
- and the script starts again.
- Pattern space is printed (unless
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. [...]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论