【发布时间】:2020-03-27 15:19:02
【问题描述】:
例如,有一个名为header.txt的文件,下面是这个文件的内容:
this is the header line after an empty line
this is the data line
this is the last line after an empty line
当我运行sed -n '/^this/{N;p}' header.txt 时,结果是:
this is the header line after an empty line
this is the data line
但是如果我在没有花括号的情况下运行sed -n '/^this/N;p' header.txt,结果是:
this is the header line after an empty line
this is the data line
为什么结果如此不同? sed中的{}有什么用?
【问题讨论】:
-
@WiktorStribiżew,嗯。在这个问题中预设了这个问题的答案;他们已经知道这种行为,但只询问命名法。老实说,我对另一个问题的判断是it, like other nomenclature questions, is off-topic。
-
您的问题得到了很好的回答,但如果您正在考虑在 sed 的上下文中使用
{}(而不是作为 RE 间隔),那么您使用的是错误的工具 - 一个awk 解决方案将是更清晰、更简单、更高效、更便携、更健壮和更易于增强/维护的某种组合。如果您发布一个新问题,询问如何使用标准 UNIX 工具做任何您想做的事情,那么我希望您也会得到一个很好的答案。