Sed regex to inanimate replace?

I know another question that is very similar, but for some reason I'm still having problems.

I have a GC log that I am trying to crop in 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 am applying s/\[Tenured:.*\]//

And as expected, the result is greedily clipped to the rest of the line:

 63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546: 

So, let's try and be greedy not to match the closing right bracket with s/\[Tenured:[^\]]*\]// , but, alas, no match will be made, and sed will skip the line, creating 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 am I not greedily matching and replacing this section? Thanks,

+4
source share
4 answers

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

The manual says:

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

i.e. In this context, no backslash is required.

  • Raz
+5
source
 sed -e 's/\[Tenured:[^]]*\]//' 

Apparently you should not hide the square bracket. Unusual!

From man re_format :

The parenthesis expression is a list of characters enclosed in '[]' ... To include the literal ']' in the list, make it the first character (after the possible β€œ^”).

+3
source

Try it .*? for an undesirable option .* . (Not sure if it is supported in the sed regex engine or not, but worth a try.)

Edit: this previous SO question might be appropriate - Unwanted regex matching in sed?

0
source

This worked:

 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 -es/\\[Tenured:[^\]]*\\]// 
0
source

All Articles