Grep Console How-to #7: Rewriting Lines

As of Grep Console 3.3, lines filtered to the Grep View can be rewritten. For example, information from your log4j output that’s irrelevant during development could be removed. Or if you put some keywords in your debug output solely to have something for Grep Console to recognise, you could strip that from your Grep View output. These rewritten lines are not subject to the standard styles applied by Grep Console via regular expression matching. Instead, you can specify the styles for your rewritten lines directly.

Simple rewriting

Let’s say you have log4j prefix your error messages with the string [ERROR]. You want to filter those lines to Grep View, and you want to highlight them with a red background. But since you’re highlighting them, there’s no reason to keep the [ERROR] tag, so you want to filter that out.

This is easily done. We’ll start with an expression that matches your error lines:

\Q[ERROR] \E(.*)

This captures strings starting with the [ERROR] tag followed by a space and any number of additional characters. These additional characters are also marked as a capture group, because we still need them for our rewritten line.

Now we can add a rewrite expression. In this case, it’s simple – we only want to output the content of our capture group, so the rewrite expression looks like this:

{1}

After entering the rewrite expression, the style/link table will show an additional line labelled Whole rewritten line, which we can use to assign a red background to our rewritten line:

Simple rewriting settings

And here’s some sample output, with the regular console on the left and the Grep View output on the right:

Simple rewriting result

Styles and links

We don’t have to base the visible content of our rewritten line on the original console output. For example, if our application writes lots of very long HTTP links to the console, we actually might decide to not display the URLs, and instead just show a short text with a highlight and a clickable link that opens the URL in a browser.

A regular expression for this scenario might look like this:

(\Qhttp://\E\S*)

This matches strings starting with the prefix http:// followed by any number of non-whitespace characters. This whole URL is marked as a capture group.

We’ll replace the entire line with a simple string via the following rewrite expression:

Click (here)

Note that we’ve marked the word “here” as a group in the expression. This adds yet another line to the style/link view, labelled Rewritten group 1:

Links settings

We can use this group to assign a highlight style to the word “here”. And we’ll add a link of type URL, using {1} as the link’s URL. This will use capture group from the regular expression as the link target – and that capture group contains exactly the HTTP link from the original line.

This is what our rewritten line looks like when holding the CTRL key and hovering the mouse cursor over the link:

Links result

 

This entry was posted in Grep Console and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *