将以
* foo
开头的所有行打印到以 *
开头的任何下一行
我认为这会起作用,但它只打印第一行:
awk '/^\* foo/,/^\* /' file.org
结果:
* foo
示例文件(组织模式):
* foo
** subheading
- item 1
- item2
comments
* bar
** subheading
more text
awk '/^\* foo/,/^\* /' file.org
观察到任何匹配
/^\* foo/
的行也匹配 /^\* /
,因此打印在同一行打开和关闭,您应该通知 GNU AWK
结尾是不是以 * foo
开头而是以 开头的行*
您可以按照以下方式进行
awk '/^\* foo/,!/^\* foo/&&/^\* /' file.org
说明:
!
为否定(逻辑非),&&
为逻辑与,对于file.org
内容
* foo
** subheading
- item 1
- item2
comments
* bar
** subheading
more text
输出为
* foo
** subheading
- item 1
- item2
comments
* bar
(在 GNU Awk 5.1.0 中测试)