Splunk Search

Field extraction from data within backslashes

mbasharat
Builder

Hi,

I have dateset that contains IP addresses. IP Addresses are coming in variations due to ranges they are assigned to separated by \ backslashes. I need them to be extracted in multiple fields regardless of how many variations are there. See sample data below:

1.2.3.4\n4.5.6.7\n8.9.1.2
1.2.3.4\n4.5.6.7\n
1.2.3.4\n4.5.6.7
1.2.3.4\n4.5.6.7\n8.9.1.2

I need them like:
1.2.3.4\n4.5.6.7\n8.9.1.2
Value1: 1.2.3.4
Value2: 4.5.6.7
Value3: 8.9.1.2
Value4: and so on.....

So basically all values within backslash, I need them separated out in fields. Also, the letter "n" or any alphabets attached to any IP also needs to go.

Thanks in-advance!

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

The IP addresses are not separated by backslashes, they're separated by newlines (\n). To split them up, use the split command followed by mvexpand. Assuming the addresses are in a field called 'foo', this should do it.

... | eval ips = split(foo, "\n") | mvexpand ips
---
If this reply helps you, Karma would be appreciated.

View solution in original post

0 Karma

to4kawa
Ultra Champion
| makeresults 
| eval _raw="1.2.3.4\n4.5.6.7\n8.9.1.2
1.2.3.4\n4.5.6.7\n
1.2.3.4\n4.5.6.7
1.2.3.4\n4.5.6.7\n8.9.1.2"
| multikv noheader=t 
| fields _*
| rename COMMENT as "this is sample, check this. from here, the logic"
| rex max_match=0 "(?<data>[\d.]+)"
| streamstats count as session
| mvexpand data
| streamstats count as cols by session
| eval cols="col".cols
| xyseries session cols data
0 Karma

mbasharat
Builder

Both yours and Rich's solutions work great but rich's solution is less typing :). Accepting Rich's and upvoting t04kawa! 🙂

Thank you both!

0 Karma

richgalloway
SplunkTrust
SplunkTrust

The IP addresses are not separated by backslashes, they're separated by newlines (\n). To split them up, use the split command followed by mvexpand. Assuming the addresses are in a field called 'foo', this should do it.

... | eval ips = split(foo, "\n") | mvexpand ips
---
If this reply helps you, Karma would be appreciated.
0 Karma

mbasharat
Builder

I used the same eval but with backslash only and it did not work and I have no idea why, added \n instead like yours and worked perfectly. THANKS!!

BTW, do you happen to know the reason why just backslash does not get picked up?

Both yours and @to4kawa solutions work great but rich's solution is less typing :). Accepting RIch's and upvoting t04kawa! 🙂

Thank you both!

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Backslash by itself is not picked up because it is the escape character. That means it modifies the character that follows it. To match the literal backslash you must escape it \\\.

---
If this reply helps you, Karma would be appreciated.
0 Karma

mbasharat
Builder

Works. Thanks!!!

0 Karma

mbasharat
Builder

One more thing to add, below also worked:

| makemv delim="\n" IP_Extracted
| mvexpand IP_Extracted

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!

Kick the Tires Before You Commit: A Hands-On Tour of the Splunk Observability Cloud ...

Evaluating an enterprise observability platform usually goes like this: fill out a form, get a free trial with ...

Deep insights, no barriers: Splunk Observability Cloud Free Edition

As software delivery cycles continue to accelerate, observability shouldn’t be a luxury — it should be a ...

Monitoring AI Agents with Splunk Observability Cloud

Let’s say I’m running a travel planning AI app in production. A user asks for three concise hotel options in ...