“-n”的实际含义是什么?在 sed 中?
根据 http://linux.about.com/od/commands/l/blcmdl1_sed .htm
禁止自动打印图案 空间
我已经测试过使用或不使用 -n
, sed 都会产生相同的结果
我不明白它是什么意思。
According to http://linux.about.com/od/commands/l/blcmdl1_sed.htm
suppress automatic printing of pattern
space
I've tested with or without -n
, sed will produce same result
I dont understand what space does it means.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Sed 有两个地方来存储文本:模式空间和保存空间。模式空间是每一行被 sed 命令处理的地方;保留空间是放置一些稍后可能需要使用的文本的辅助位置。您可能只会使用模式空间。
在 sed 处理一行之前,它被放入模式空间中。然后,sed 将所有命令(例如
s///
)应用于模式空间,并且默认情况下打印模式空间中生成的文本。假设我们有一个文件myfile
,其行如下:我们运行以下命令:
Sed 将应用
s/fox/coati/
,然后应用s/dog /dingo/
文件的每一行 - 在本例中,这是我们上面显示的唯一一行。当它发生时,它将将该行放入模式空间中,其中将包含以下内容:然后,sed 将运行第一个命令。 sed 运行命令
s/fox/coati/
后,模式空间的内容将为:然后 sed 将应用第二个命令
s/dog/dingo/
。之后,模式空间的内容将是:请注意,这仅发生在内存中 - 现在没有打印任何内容。
毕竟,命令已应用于当前行,默认,sed 将获取模式空间的内容并将其打印到标准输出。但是,当您将
-n
作为 sed 的选项时,您会要求 sed 不要执行最后一步 - 除非明确要求。因此,如果您运行,则不会打印任何内容。
但是如何明确请求 sed 打印模式空间呢?好吧,您可以使用
p
命令。当 sed 找到这个命令时,它会立即打印模式空间的内容。例如,在下面的命令中,我们请求 sed 在第一个命令之后打印模式空间的内容:结果将是
注意,只有
fox
被替换。发生这种情况是因为在打印模式空间之前没有执行第二个命令。如果我们想在两个命令之后打印模式空间,我们只需将p
放在第二个命令之后:如果您使用
s///
命令,另一个选项是将p
标志传递给s///
:在这种情况下,仅当执行了标记的替换时才会打印该行。它可能非常有用!
Sed has two places to store text: pattern space and hold space. Pattern space is where each line is put to be processed by sed commands; hold space is an auxiliary place to put some text you may want to use later. You probably will use only pattern space.
Before sed goes to process a line, it is put in the pattern space. Then, sed applies all commands (such as
s///
) to de pattern space and, by default, prints the resulting text from the pattern space. Let us suppose we have a filemyfile
with a line like:We run the following command:
Sed will apply
s/fox/coati/
and thens/dog/dingo/
for each line of the file - in this case, the only one we showed above. When it occurs, it will put the line in the pattern space, which will have the following content:Then, sed will run the first command. After sed runs the command
s/fox/coati/
, the content of the pattern space will be:Then sed will apply the second command,
s/dog/dingo/
. After that, the content of the pattern space will be:Note that this only happens in memory - nothing is printed by now.
After all, commands have been applied to the current line, by default, sed will then get the content of the pattern space and print it to the standard output. However, when you give
-n
as an option to sed, you ask sed not to execute this last step — except if it is explicitly required. So, if you runnothing will be printed.
But how could you explicitly request sed to print the pattern space? Well, you can use the
p
command. When sed finds this command, it will print the content of the pattern space immediately. For example, in the command below we request sed to print the content of the pattern space just after the first command:The result will be
Note that only
fox
is replaced. It happens because the second command was not executed before the pattern space was printed. If we want to print the pattern space after both commands, we just putp
after the second one:Another option, if you are using the
s///
command, is to pass thep
flag tos///
:In this case, the line will only be printed if the flagged replacement was executed. It may be very useful!
只需尝试 sed 不执行任何操作:
第一个
将打印整个文件,但第二个将不会打印任何内容。
Just try a sed do-nothing:
and
First will print whole file but second will NOT print anything.
除非由
p
命令明确指定:例如,如果您想使用 sed 模拟 grep 的操作:
这会将 sed 置于安静模式,其中 sed 将抑制所有输出,
-n
你会得到一个 c 的出现(以及两次 a 和 b 的出现)This puts sed into quiet mode, where sed will suppress all output except for when explicitly stated by a
p
command:An example of this would be if you wanted to use sed to simulate the actions of grep:
without the
-n
you would get an occurrence of c (and two occurrences of a and b)模式空间是隐式打印的。
$ echo "abc d" |
$ echo "abc d" | sed -n "s/a/apple/"
无输出。
显式打印模式空间。
The pattern space is printed implicitly.
$ echo "a b c d" | sed -n "s/a/apple/"
No output.
Explicitly print the pattern space.