在将sed
命令与-n
选项和a
命令一起使用时,我遇到了一些意外的行为。根据sed
手册,-n
选项应该在脚本的每个循环结束时抑制模式空间的自动打印。但是,当我运行以下命令:
echo a | sed -n -e ‘$a b’
字符串
它打印b
,即使我希望它什么也不打印。
我尝试运行echo a | sed -n -e '$a b'
命令,由于-n
选项,我希望它什么也不打印。但是,它打印了b
,这是出乎意料的。如果这是GNUsed
在一起使用这些选项时的预期行为,那就意味着我误解了它们应该如何工作。有人能解释一下为什么会这样吗?
2条答案
按热度按时间jljoyd4f1#
字符串
印刷品
型
和/或
型
印刷品
型
其示出了当指定
-n
时,不打印 * 图案空间 * 的内容。fdx2calv2#
为了扩展Diego的优秀答案,
-n
选项基本上说“除非我要求什么否则闭嘴”,但如果它完全抑制了所有输出,它将是无用的,是吗?基本上,任何时候你 * 告诉 *
sed
你想要输出,它仍然会给予它。字符串
希望能帮上忙。