Splunk Search

Regex to find a directory in a path

rileyken
Explorer

I have these paths as sources for an index (the paths are linux file system paths)

/usr/local/myfiles1/myfacilityA/Debug/ADT/ADT_2019-07-30.txt
/usr/local/myfiles2/myfacilityB/Debug/ADT/ADT_2019-07-30.txt
/usr/local/myfiles1/myfacilityC/Debug/ADT/ADT_2019-07-30.txt

i would like to be able to extract "myfacility" as a field so I can search for all of the events with that source path.

It would be fantastic to just capture everything between the /usr/local/myfiles? and the /Debug/ADT/ADT....

Tags (1)
0 Karma
1 Solution

jaime_ramirez
Communicator

For myfacility field:

...
| rex field=source "\/usr\/local\/myfiles\d+\/myfacility(?<myfacility>.*?)\/Debug\/ADT\/"

And for everything between the /usr/local/myfiles? and the /Debug/ADT/ADT:

...
| rex field=source "\/usr\/local\/myfiles(?<myfacilityPath>.*?)\/Debug\/ADT\/"

Hope it helps

View solution in original post

0 Karma

bwlm
Path Finder

If you would like to avoid cumbersome regex rules and are just looking for the fourth directory (or 0-4 directory path) you may want to consider the Splunk Multivalue eval functions "mvindex", "split", and "mvjoin" to do the work for you... note that the "nomv" commands are optional in most cases.

 

| makeresults
| eval filepathname="/usr/local/myfiles1/myfacilityA/Debug/ADT/ADT_2019-07-30.txt"
| eval Dir4=mvindex(split(filepathname,"/"),4,4) 
| nomv Dir4
| eval Dir0to4=mvjoin(mvindex( split(filepathname,"/"),0,4), "/") 
| nomv Dir0to4

 

 

0 Karma

rileyken
Explorer

This was just what I was looking for! Thanks Jamie

0 Karma

jaime_ramirez
Communicator

For myfacility field:

...
| rex field=source "\/usr\/local\/myfiles\d+\/myfacility(?<myfacility>.*?)\/Debug\/ADT\/"

And for everything between the /usr/local/myfiles? and the /Debug/ADT/ADT:

...
| rex field=source "\/usr\/local\/myfiles(?<myfacilityPath>.*?)\/Debug\/ADT\/"

Hope it helps

0 Karma
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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...