Getting Data In

Extract filename from source

vincenty
Explorer

I am trying do a search for all exceptions and list the associated filename instead of the whole path+filename in my results table.

I tried the following in my search but it didn't work:
Exception sourcetype=qagadc1 | rex ".?(?(?:\w+.)+\w?Exception)." | rex field=source "(?(/\w)+)/+(?\w+)+.*" |table source fname

my source structure is not unique, they are varies as follow:
/home/d1/d2/d3/fn1.log
/home/d1/d2/d3/d4/d5/fn2.out
/home/d1/d2/d3/d4/d5/d6/fn3.log

...

please help.

0 Karma

koshyk
Super Champion

In case if your filename contains a . then

# For Unix style
| rex field=<fieldname> "/(?<newField>[\w\d\.]+$)"
0 Karma

martin_mueller
SplunkTrust
SplunkTrust

That's just a matter of modifying the expression, see my edited answer.

0 Karma

vincenty
Explorer

actually, I am trying to get the filename extension (i.e fn2.out, fn3.log) as part of the "fname" returned.

Tried appending what you suggested still does not give me fn3.log, or fn2.out etc.

0 Karma

vincenty
Explorer

actually, I am trying to get the filename extension (i.e fn2.out, fn3.log) as part of the "fname" returned.

Tried appending what you suggested still does not give me fn3.log, or fn2.out etc.

0 Karma

vincenty
Explorer

actually, I am trying to get the filename extension (i.e fn2.out, fn3.log) as part of the "fname" returned.

Tried appending what you suggested still does not give me fn3.log, or fn2.out etc.

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

To extract the last segment of a path from a field you can append this to your search:

 | eval field = replace(field, ".*/", "")

Edit: If you want the last bit after the final dot you can modify the expression like this:

  | eval field = replace(field, ".*\.", "")
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...