Splunk Search

How to write the regex to remove the filename and only return the path from my data?

kleckns
Explorer

I'm trying to remove the filename from path and return path only in Splunk

example:

`C:\path\path\path\path\application.exe`

the path is variable length.

`| regex mypath="[^.*\/].*" |`

not only does my syntax for the command between splunk pipes not work, but the syntax for regex argument is incorrect as well. any help appreciated.

Tags (2)
1 Solution

somesoni2
Revered Legend

Give this a try

| rex field=yourfieldname  "(?<Path>.*)\\\\"

See this runanywhere splunk search

| gentimes start=-1 | eval path="C:\path\path\path\path\application.exe" | table path | rex field=path "(?<Path>.*)\\\\"

View solution in original post

judevine
Explorer

You probably already have your solution in 2021, but anyway, here's a solution:

| makeresults
| eval path="/opt/splunk/etc/system/local/inputs.conf"
| eval base_path=replace(path,"\/[^\/]+\.[^\/]+$","")
| table path base_path

0 Karma

somesoni2
Revered Legend

Give this a try

| rex field=yourfieldname  "(?<Path>.*)\\\\"

See this runanywhere splunk search

| gentimes start=-1 | eval path="C:\path\path\path\path\application.exe" | table path | rex field=path "(?<Path>.*)\\\\"

kleckns
Explorer

That worked. Thank you very much.

0 Karma
Get Updates on the Splunk Community!

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...