如何打印两个括号之间的内容。
例如:
return [ "server1.domain.com" ,
"server2.domain.com" ,
"server3.domain.com" ]
预期结果:
"server1.domain.com" ,
"server2.domain.com" ,
"server3.domain.com"
如何得到预期的结果?
答案1
和pcregrep
:
pcregrep -Mo 'return \[\K[^]]*'
Multiline 匹配 onreturn [
后跟一系列非]
字符,但仅o将部分输出到Keep (位于 的右侧\K
)。
答案2
和sed
sed '
/.*\[ /{
s/// #remove all upto open square bracket
:a #return mark
/\ ].*/!{
N #get next line untill there is close square bracket
ba #back to return mark
}
s/// #remove all from close square bracket to end of line
}
'