Splunk Search

Eval Match function Issue

vikasreddy
Explorer

I need to extract the date from the file name,But the format of the data on different files are different for eg:D20171202 is one file format,other one is D171203. I am extracting the date using regex and using eval match function I am changing the date format it is working fine , but it is changing some files date only.

below is the code I used
index=xyz source=a
| rex field=oldsorcefile"._D(?\d{8})."
|eval FDate2=case(macth(FDate1,"20*")FDate1,match(FDate,"17*"),("20"+(FDate1)))
| table Fdate1,FDate2

Here is the Sample output...

FDATE1 FDATE2
17071318 2017071318
17071418 2017071418
20170714 20170714
20170712 20170712
17071216 17071216

it is working fine but some dates are not changing according to code. any help would be great full.
Thanks

Tags (3)
0 Karma
1 Solution

niketn
Legend

Based on your description Date can be 6 digits (YYMMDD) or 8 digits (YYYYMMDD). Which is followed by ._D in the filename.
I expect your existing logic to fail when you have two 20s or two 17s in the date like 20170220 or 170117 etc.

One of the ways would be to match ^20 and ^17 in your existing eval so that it finds the pattern only in the beginning of the string i.e. : | eval FDate2=case(match(FDate1,"^17","20".FDate1,FDate1)

Other option would be perform eval based on length of extracted date field and to prefix 20 to date if length of date field in file is 6 and not 8 (following is run anywhere search to test the same):

| makeresults
| eval oldsourcefile="blahblah._D20170118"
| rex field=oldsourcefile ".D(?<FDate1>\d{6,8})"
| eval FDate2=if(len(FDate1)==6,"20".FDate1,FDate1)
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

0 Karma

vikasreddy
Explorer

Ya, it worked Thank you Niketnilay.

0 Karma

niketn
Legend

@vikasreddy, have you tried the option suggested by DalJeanis, it employs single rex command with sed to format field as per you need. I think that is a better option.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

vikasreddy
Explorer

@Niketnilay,Ya it is working for me, I have tried your options it is working later, I have made some tweaks in my code suggested by DalJeanis.

0 Karma

niketn
Legend

@vikasreddy, if DalJeanis' answer also helped, please upvote his comment. I have already done the same.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

DalJeanis
SplunkTrust
SplunkTrust

try this -

| makeresults 
| eval _raw = "D171228  other stuff D20170521  more stuff D180521" 
| rename COMMENT as "The above just makes test data for correcting the _raw"

| rename COMMENT as "This changes all 6-digit dates in the _raw starting with D17 through D19 to D2017 through D2019"
| rename COMMENT as "so the solution will not break for over two years"
| rex field=_raw mode=sed "s/\bD(1[7|8|9]\d{4})\b/D20\1/g"


| makeresults 
| eval mydate = "D171228 D20170521 D180521" 
| makemv mydate 
| mvexpand mydate
| rename COMMENT as "The above just makes test data for changing a specific already-extracted date field."

| rename COMMENT as "This changes all 6-digit dates in that field starting with D17 through D19 to D2017 through D2019"
| rename COMMENT as "so the solution will not break for over two years"
| rename COMMENT as "this will work on a multivalue field as well, to demo just remove the mvexpand command"
| rex field=mydate mode=sed "s/^D(1[7|8|9]\d{4})$/D20\1/g"

vikasreddy
Explorer

Thank you DalJeanis, For you quick reply. It was awesome the whole thing can be done with single line.

niketn
Legend

Based on your description Date can be 6 digits (YYMMDD) or 8 digits (YYYYMMDD). Which is followed by ._D in the filename.
I expect your existing logic to fail when you have two 20s or two 17s in the date like 20170220 or 170117 etc.

One of the ways would be to match ^20 and ^17 in your existing eval so that it finds the pattern only in the beginning of the string i.e. : | eval FDate2=case(match(FDate1,"^17","20".FDate1,FDate1)

Other option would be perform eval based on length of extracted date field and to prefix 20 to date if length of date field in file is 6 and not 8 (following is run anywhere search to test the same):

| makeresults
| eval oldsourcefile="blahblah._D20170118"
| rex field=oldsourcefile ".D(?<FDate1>\d{6,8})"
| eval FDate2=if(len(FDate1)==6,"20".FDate1,FDate1)
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Get Updates on the Splunk Community!

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...