Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 [].

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:.*\]//

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:

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,

like image 879
Jé Queue Avatar asked Mar 01 '23 02:03

Jé Queue


1 Answers

Almost: s/\[Tenured:[^]]*\]//

The manual says:

To include a literal ']' in the list, make it the first character (following a possible '^').

i.e. No backslash is required in this context.

  • Raz
like image 168
Roland Turner Avatar answered Mar 12 '23 13:03

Roland Turner