Splunk Search

Preventing format from being called on a subsearch

cphair
Builder

Hello,

I have a macro (a subsearch enclosed in square brackets) that I use to filter my initial search. I would like to do some regex magic on the search string that format creates. Unfortunately, if I call format and do parsing on the search field, a second format seems to be implicitly called at the end of the macro, and it encloses the regexed search string in an extra set of quotes and double parentheses, which confuses the outer search. Is there a way either to prevent format from being called at all, or to keep it from enclosing the field in quotes?

Tags (3)
0 Karma
1 Solution

cphair
Builder

Figured it out. I can just call return at the end of the macro and it doesn't reapply the formatting.

View solution in original post

0 Karma

cphair
Builder

Figured it out. I can just call return at the end of the macro and it doesn't reapply the formatting.

0 Karma

cphair
Builder

Actually, if I use index=null splunk_server=localhost | stats count, that returns relatively quickly--it's the going out to the distributed search peers that makes it take forever. But at any rate, getting the returned macro string correct is my bigger concern.

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

That's odd on the stats, provided you have no search in front of it the stats just has to go "Oh - no events, print out count=0 and be done!" in no time at all.

0 Karma

cphair
Builder

stats count actually takes several seconds to return a single event. I can't write the macro as a single eval statement because of the regex requirements, and I have never gotten eval-based macros to work in a more complicated format.

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

Hmm. Two thoughts - first, you can replace dummy search | head 1 with stats count to use up zero resources whatsoever and second, have you considered using eval-based macros instead of the subsearch?

0 Karma

cphair
Builder

I'm playing with parsing input from a dashboard textbox. It's something like this.

[dummy search| head 1 | eval foo="$input$" | rex field=foo "(?stuff)(?morestuff)" | table field1, field2 | format | rex field=search mode=sed "regexery"]

If I run it in the search bar without the brackets and paste the resulting query in my outer search, it works fine. When I call it as a macro, it doesn't. If I run it in the search bar with the square brackets included, it adds an extra ((" and ")) on either side of the string, which I'm guessing is how the search sees it.

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

When I do this

[gentimes start=-1 increment=8h | fields starthuman | format | eval search = replace(search, "\(", "{") | eval search = replace(search, "\)", "}")]

there is no extra format being called, and splunk's litsearch literally does look for curly braces - what are you doing differently?

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Index This | What travels the world but is also stuck in place?

April 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Discover New Use Cases: Unlock Greater Value from Your Existing Splunk Data

Realizing the full potential of your Splunk investment requires more than just understanding current usage; it ...

Continue Your Journey: Join Session 2 of the Data Management and Federation Bootcamp ...

As data volumes continue to grow and environments become more distributed, managing and optimizing data ...