如何使用方括号搜索多行的开始和结束

如何使用方括号搜索多行的开始和结束

文字有

[{hello=(1+2-0), 
hello2 = 1}]

但当以 [ 开头并以 ] 结尾时,它们位于不同的行中

如何提取类似 grep 的这种多行模式?

答案1

进行多行 grep 类匹配的一个直接方法是pcregrepuniverse存储库安装。

例如给定

$ cat file
Text have

[{hello=(1+2-0), 
hello2 = 1}]
But are in two or three multiple lines when start with [ and end with ] are in different line

How to extract like grep for this multiple line pattern?

然后

$ pcregrep -M '(?s)\[.*?\]' file
[{hello=(1+2-0), 
hello2 = 1}]
But are in two or three multiple lines when start with [ and end with ] are in different line

相关内容