grep 是否可以将其匹配项打印到多行,即使在同一行找到?

Can grep print its matches to multiple lines, even if found on the same line?

例如,使用以下字符串:

[:variable_one] == options[:variable_two]

和以下 grep 参数:

grep -Eo "\[\:.*?\]"

它将显示输出:

[:variable_one] == options[:variable_two]

但是,我希望获得以下输出:

[:variable_one]
[:variable_two]

有没有办法将每个匹配项“拆分”成单独的一行,即使它在一行中找到多个匹配项?基本上是在寻找与此相反的答案:

:](不是括号表达式的一部分)字符在正则表达式模式中并不特殊。 *? 在 POSIX ERE 模式中被视为 *,因此它过于贪婪并匹配直到最右边出现 ]

grep 一起使用的 POSIX BRE 兼容正则表达式看起来像

#!/bin/bash
s='[:variable_one] == options[:variable_two]'
grep -o "\[:[^][]*]" <<< "$s"

online demo。输出:

[:variable_one]
[:variable_two]