Splunk Search

truncating and merge values

exocore123
Path Finder

I have a bunch of log error descriptions that have unique IDs at the end of the sentences

"CC declined. 123" 1
"CC declined. 456" 1
"Some error. 921" 1
"Some error. 222" 1

I want to truncate the rest of the numbers and merge the count so the result looks like

"CC decined" 2
"Some error" 2

In addition, is there a way to automatically do this without stating every single instances that I want to truncate?

0 Karma
1 Solution

woodcock
Esteemed Legend

Like this:

|makeresults | eval raw="CC declined. 123,1
CC declined. 456,1
Some error. 921,1
Some error. 222,1
Failed server 2,1
Failed server 12,1
CC declined. x12,1"
| makemv delim="
" raw
| mvexpand raw
| rename raw AS _raw
| rex "^(?<description>.*?),(?<count>\d+)$"
| table description count

| rename COMMENT AS "Everything above generates sample event data; everything below is your solution."

| rex field=description mode=sed "s/\s+\d+$//"
| stats sum(count) AS count BY description

View solution in original post

0 Karma

woodcock
Esteemed Legend

Like this:

|makeresults | eval raw="CC declined. 123,1
CC declined. 456,1
Some error. 921,1
Some error. 222,1
Failed server 2,1
Failed server 12,1
CC declined. x12,1"
| makemv delim="
" raw
| mvexpand raw
| rename raw AS _raw
| rex "^(?<description>.*?),(?<count>\d+)$"
| table description count

| rename COMMENT AS "Everything above generates sample event data; everything below is your solution."

| rex field=description mode=sed "s/\s+\d+$//"
| stats sum(count) AS count BY description
0 Karma

exocore123
Path Finder

Seems like your regex matches whitespace then number, can i terminate the moment I see a period/any special characters

0 Karma

woodcock
Esteemed Legend

It strips off all trailing digits and the white space in front of it.

0 Karma

exocore123
Path Finder

Yes, but is there a pattern to strip the moment a special character is seen, strip the rest of the string. Or strip off starting from the special character too is fine

0 Karma

exocore123
Path Finder

| rex field=description "^(?<description>[^\r\.]+)" something like this, but for many special characters

0 Karma

woodcock
Esteemed Legend

Yes, like this:

| rex field=description mode=sed "s/[specialcharacterlisthere].*$//"
0 Karma

DalJeanis
Legend

try this, where desc is the field name.

| rex field=desc "^(?<desc>(?i)[\w\s]+?)[^\w]?\s*\w?\d+$"

The complication comes from that x12 at the end of one, with no special characters in it.

Assumptions: the last character of the description field will always be a number.

0 Karma

exocore123
Path Finder

What about matching the first special character/non-word character?

0 Karma

exocore123
Path Finder

I used | rex field=description "^(?<description>[^\r\.]+)", but I am getting "Unexpected closed tag"

0 Karma

woodcock
Esteemed Legend

Show us a diverse set of raw events and the separated out description filed values.

0 Karma

exocore123
Path Finder
description         count
"CC declined. 123"     1
"CC declined. 456"     1
"Some error. 921"      1
"Some error. 222"      1
"Failed server 2"      1
"Failed server 12"     1
"CC declined. x12"     1

to

     description         count
"CC declined"     3
"Some error"      2
"Failed server"      2

I guess can we automatically truncate the string the moment we see any type of special character?

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!

Event Series: Level up your SOC: Advancing with Splunk Enterprise Security

AI has fundamentally raised the stakes for security operations, and this three-part series is your guide to ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

SOC4Kafka - New Kafka Connector Powered by OpenTelemetry

The new SOC4Kafka connector, built on OpenTelemetry, enables the collection of Kafka messages and forwards ...