Getting Data In

rex syntax to parse source path for a sub-directory name and the file name

vincenty
Explorer

I am trying to parse source path for a sub-directory name and its file name. My source files are as follows:

source=/home/sd1/sd2/sd3/(subdir1)/file1.out
source=/home/sd1/sd2/sd3/(subdir1)/sd4/file2.log
source=/home/sd1/sd2/sd3/(subdir1)/sd4/sd5/file3.out
source=/home/sd1/sd2/sd3/(subdir1)/sd4/sd5/sd6/file4.out

I can't seem to get the syntax correct with the following:

Exception sourcetype=EDGDC2 | rex ".*?(?<exception>(?:\w+\.)+\w*?Exception).*" | rex field=source "(/\w*)+(/\w*)+(/\w*)+(/\w*)+(?<subdir1>(/\w*))+/+(?<fname>(\w*+\.+\w*))+" | stats count by exception, subdir1

The above rex will always get the last sub-directory where the file is. Tried several different variation but the above is the closest I get...

Tags (3)
0 Karma

kristian_kolb
Ultra Champion
rex field=source "(/[^/]+){4}/(?<subdir1>[^/]+)/.*/(?<fname>.*)$"

should work.

EDIT: missed that you wanted the filename as well...

/k

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...

Data Persistence in the OpenTelemetry Collector

This blog post is part of an ongoing series on OpenTelemetry. What happens if the OpenTelemetry collector ...

Thanks for the Memories! Splunk University, .conf25, and our Community

Thank you to everyone in the Splunk Community who joined us for .conf25, which kicked off with our iconic ...