Splunk Search

How to do a field extraction on the date string of a Windows source path field?

shariinPH
Contributor

Hi All

I have here a sample source path:

source="G:\\OperationData\\Ntr_RdngANI\\Work_Bill\\Dis_Rec\\03_12\\Fun_20150312_successful.txt"

I want to extact the 20150312

how can I do that?
I already tried

|rex field=source "G:\\OperationData\\Ntr_RdngANI\\Work_Bill\\Dis_Rec\\03_12\\Fun_(?\d+)_successful.txt"

but it doesn't work, I think its about the "\"

can someone help me with this?
Thanks!

1 Solution

jeffland
SplunkTrust
SplunkTrust

I believe there are two errors in your expression: a) you need to escape your backslashes, and b) you did not give your capture group a name. I can reccomend https://regex101.com/ to confirm your regular expressions.

In your case, your path contains double backslashes and you need to escape both of them, so your regex looks like | rex field=source "G:\\\\OperationData\\\\Ntr_RdngANI\\\\Work_Bill\\\\Dis_Rec\\\\03_12\\\\Fun_(?\d+)_successful.txt" - the capture in angular brackets being the name of your capture group, and \\\\ as a literal \\.

Hope this helps. And you should really give the page mentioned above a try!

View solution in original post

vincenteous
Communicator

Hi shariin,

What jeffland said about double backslashes and naming your capturing group was correct. Instead of using the full path as regex pattern, why don't you focus on the file name instead? Something like this:

Fun_(?<DateLog>\d{8})_.*

I've tested it and you can get what you wanted.

masonmorales
Influencer

^Easiest solution

0 Karma

shariinPH
Contributor

Yes! This works! Thanks!! 🙂

0 Karma

jeffland
SplunkTrust
SplunkTrust

I believe there are two errors in your expression: a) you need to escape your backslashes, and b) you did not give your capture group a name. I can reccomend https://regex101.com/ to confirm your regular expressions.

In your case, your path contains double backslashes and you need to escape both of them, so your regex looks like | rex field=source "G:\\\\OperationData\\\\Ntr_RdngANI\\\\Work_Bill\\\\Dis_Rec\\\\03_12\\\\Fun_(?\d+)_successful.txt" - the capture in angular brackets being the name of your capture group, and \\\\ as a literal \\.

Hope this helps. And you should really give the page mentioned above a try!

shariinPH
Contributor

Hello @jeffland .. This works too ! 🙂 thanks!! 🙂

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...