[英]sed regex to non-greedy replace?
I am aware of another question that is quite similar, but for some reason I'm still having problems. 我知道另一个非常相似的问题,但由于某种原因,我仍然遇到问题。
I have a GC log that I'm trying to trim out the Tenured section enclosed in []
. 我有一个GC日志,我正试图修剪
[]
的Tenured部分。
63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546: [Tenured: 1416K->1065K(1536K), 0.0492621 secs] 1922K->1065K(2112K), 0.0513331 secs]
I apply s/\\[Tenured:.*\\]//
我申请
s/\\[Tenured:.*\\]//
And quite expectantly, the result is trimmed greedily through the remainder of the line: 并且非常期待,结果在整个线路的其余部分贪婪地修剪:
63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546:
So let's try and be non-greedy not match a closing right bracket with s/\\[Tenured:[^\\]]*\\]//
but alas no match is made and sed skips over the line, producing the same original output: 所以,让我们尝试非贪婪不匹配右侧括号与
s/\\[Tenured:[^\\]]*\\]//
但是没有匹配,sed跳过该行,产生相同的原始输出:
63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546: [Tenured: 1416K->1065K(1536K), 0.0492621 secs] 1922K->1065K(2112K), 0.0513331 secs]
How do I non-greedily match and replace that section? 我如何非贪婪地匹配和替换该部分? Thanks,
谢谢,
Almost: s/\\[Tenured:[^]]*\\]// 几乎:s / \\ [终身:[^]] * \\] //
The manual says: 手册说:
To include a literal ']' in the list, make it the first character (following a possible '^').
要在列表中包含文字“]',请将其设为第一个字符(在可能的”^“之后)。
ie No backslash is required in this context. 即在此上下文中不需要反斜杠。
sed -e 's/\[Tenured:[^]]*\]//'
Apparently you shouldn't escape the close square bracket. 显然你不应该逃避紧密的方括号。 Wacky!
古怪!
From man re_format
: 来自
man re_format
:
A bracket expression is a list of characters enclosed in '[]' ... To include a literal ']' in the list, make it the first character (following a possible `^').
括号表达式是包含在'[]'中的字符列表... 要在列表中包含文字']',请将其作为第一个字符(在可能的'^'之后)。
Try .*?
试试
.*?
for the non-greedy variant of .*
. 对于
.*
的非贪婪变体。 (Not sure if it's supported in sed
's regex engine or not, but it's worth a try.) (不确定它是否在
sed
的正则表达式引擎中得到支持,但值得一试。)
Edit: This previous SO question may be relevant - Non greedy regex matching in sed? 编辑:这个以前的SO问题可能是相关的 - 在sed中非贪婪的正则表达式匹配?
这有效:
echo "63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546: [Tenured: 1416K->1065K(1536K), 0.0492621 secs] 1922K->1065K(2112K), 0.0513331 secs]" | sed -e s/\\[Tenured:[^\]]*\\]//
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.