tl;dr
默认情况下,sed
自动将(可能已修改的)输入行打印到其输出目标,无论是隐含的还是显式指定的:默认情况下,打印到stdout(终端,除非重定向);和-i
, to a 临时文件最终替换输入文件。
In both cases, -n
抑制这种自动打印,因此 - 除非您使用显式输出函数,例如p
或者,就你而言,a
- nothing打印到标准输出/写入临时文件。
- 请注意,自动打印适用于所谓的模式空间,这就是(可能修改过的)input举行;显式输出函数,例如
p
, a
, i
and c
do not打印到模式空间(用于潜在的后续修改),它们打印直接到目标流/文件, 这就是为什么a\testing
尽管使用了-n
.
请注意,与-i
, sed
的隐式打印/显式输出命令only打印到临时文件,而不是打印到标准输出,因此使用命令-i
对于标准输出(终端)输出来说总是安静的 - 您不需要做任何额外的事情。
举一个具体的例子(GNU sed
句法)。
Since the use of -i
is incidental to the question, I've omitted it for simplicity. Note that -i
prints to a temporary file first, which, on completion, replaces the original. This comes with pitfalls, notably the potential destruction of symlinks; see the lower half of this answer https://stackoverflow.com/a/30066428/45375 of mine.
# Print input (by default), and append literal 'testing' after
# lines that contain 'please'.
$ sed '/please/ a testing' <<<$'yes\nplease\nmore'
yes
please
testing
more
# Adding `-n` suppresses the default printing, so only `testing` is printed.
# Note that the sequence of processing is exactly the same as without `-n`:
# If and when a line with 'please' is found, 'testing' is appended *at that time*.
$ sed -n '/please/ a testing' <<<$'yes\nplease\nmore'
testing
# Adding an unconditional `p` (print) call undoes the effect of `-n`.
$ sed -n 'p; /please/ a testing' <<<$'yes\nplease\nmore'
yes
please
testing
more