“-n”的实际含义是什么?在 sed 中?

发布于 2024-11-05 05:49:06 字数 271 浏览 0 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

家住魔仙堡 2024-11-12 05:49:06

Sed 有两个地方来存储文本:模式空间和保存空间。模式空间是每一行被 sed 命令处理的地方;保留空间是放置一些稍后可能需要使用的文本的辅助位置。您可能只会使用模式空间。

在 sed 处理一行之前,它被放入模式空间中。然后,sed 将所有命令(例如 s///)应用于模式空间,并且默认情况下打印模式空间中生成的文本。假设我们有一个文件 myfile ,其行如下:

The quick brown fox jumps over the lazy dog.

我们运行以下命令:

sed 's/fox/coati/;s/dog/dingo/' myfile

Sed 将应用 s/fox/coati/ ,然后应用 s/dog /dingo/ 文件的每一行 - 在本例中,这是我们上面显示的唯一一行。当它发生时,它将将该行放入模式空间中,其中将包含以下内容:

The quick brown fox jumps over the lazy dog.

然后,sed 将运行第一个命令。 sed 运行命令 s/fox/coati/ 后,模式空间的内容将为:

The quick brown coati jumps over the lazy dog.

然后 sed 将应用第二个命令 s/dog/dingo/。之后,模式空间的内容将是:

The quick brown coati jumps over the lazy dingo.

请注意,这仅发生在内存中 - 现在没有打印任何内容。

毕竟,命令已应用于当前行,默认,sed 将获取模式空间的内容并将其打印到标准输出。但是,当您将 -n 作为 sed 的选项时,您会要求 sed 不要执行最后一步 - 除非明确要求。因此,如果您运行,

sed -n 's/fox/coati/;s/dog/dingo/' myfile

则不会打印任何内容。

但是如何明确请求 sed 打印模式空间呢?好吧,您可以使用 p 命令。当 sed 找到这个命令时,它会立即打印模式空间的内容。例如,在下面的命令中,我们请求 sed 在第一个命令之后打印模式空间的内容:

sed -n 's/fox/coat/;p;s/dog/dingo/' myfile

结果将是

$ sed -n 's/fox/coati/;p;s/dog/dingo/' myfile 
The quick brown coati jumps over the lazy dog.

注意,只有 fox 被替换。发生这种情况是因为在打印模式空间之前没有执行第二个命令。如果我们想在两个命令之后打印模式空间,我们只需将 p 放在第二个命令之后:

sed -n 's/fox/coati/;s/dog/dingo/;p' myfile

如果您使用 s/// 命令,另一个选项是将 p 标志传递给 s///

sed -n 's/fox/coati/;s/dog/dingo/p' myfile 

在这种情况下,仅当执行了标记的替换时才会打印该行。它可能非常有用!

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 file myfile with a line like:

The quick brown fox jumps over the lazy dog.

We run the following command:

sed 's/fox/coati/;s/dog/dingo/' myfile

Sed will apply s/fox/coati/ and then s/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:

The quick brown fox jumps over the lazy dog.

Then, sed will run the first command. After sed runs the command s/fox/coati/, the content of the pattern space will be:

The quick brown coati jumps over the lazy dog.

Then sed will apply the second command, s/dog/dingo/. After that, the content of the pattern space will be:

The quick brown coati jumps over the lazy dingo.

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 run

sed -n 's/fox/coati/;s/dog/dingo/' myfile

nothing 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:

sed -n 's/fox/coat/;p;s/dog/dingo/' myfile

The result will be

$ sed -n 's/fox/coati/;p;s/dog/dingo/' myfile 
The quick brown coati jumps over the lazy dog.

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 put p after the second one:

sed -n 's/fox/coati/;s/dog/dingo/;p' myfile

Another option, if you are using the s/// command, is to pass the p flag to s///:

sed -n 's/fox/coati/;s/dog/dingo/p' myfile 

In this case, the line will only be printed if the flagged replacement was executed. It may be very useful!

誰認得朕 2024-11-12 05:49:06

只需尝试 sed 不执行任何操作:

sed '' file

第一个

sed -n '' file

将打印整个文件,但第二个将不会打印任何内容。

Just try a sed do-nothing:

sed '' file

and

sed -n '' file

First will print whole file but second will NOT print anything.

把回忆走一遍 2024-11-12 05:49:06

除非由 p 命令明确指定:

   -n 
   --quiet 
   --silent
          By default, sed will print out the pattern space at
          the  end  of  each cycle through the script.  These
          options disable this automatic  printing,  and  sed
          will  only  produce  output when explicitly told to
          via the p command.

例如,如果您想使用 sed 模拟 grep 的操作:

$echo -e "a\nb\nc" | sed -n '/[ab]/ p'
a
b

这会将 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:

   -n 
   --quiet 
   --silent
          By default, sed will print out the pattern space at
          the  end  of  each cycle through the script.  These
          options disable this automatic  printing,  and  sed
          will  only  produce  output when explicitly told to
          via the p command.

An example of this would be if you wanted to use sed to simulate the actions of grep:

$echo -e "a\nb\nc" | sed -n '/[ab]/ p'
a
b

without the -n you would get an occurrence of c (and two occurrences of a and b)

昔梦 2024-11-12 05:49:06
$ echo "a b c d" | sed "s/a/apple/"
apple b c d

模式空间是隐式打印的。

$ echo "abc d" | $ echo "abc d" | sed -n "s/a/apple/"

无输出。

$ echo "a b c d" | sed -n "s/a/apple/p"
apple b c d

显式打印模式空间。

$ echo "a b c d" | sed "s/a/apple/"
apple b c d

The pattern space is printed implicitly.

$ echo "a b c d" | sed -n "s/a/apple/"

No output.

$ echo "a b c d" | sed -n "s/a/apple/p"
apple b c d

Explicitly print the pattern space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文