Splunk Search

Extract field till nth repetition of a string

gowtham495
Path Finder

I have following sample event

jaskdjkasdkjas CR akjhdjhdjsdhCR 1231jljk23klj3 CR sagdiugsds 7126372 nklsdlkCR

i need to extract field upto 3rd CR

my output string should be like this : jaskdjkasdkjas CR akjhdjhdjsdhCR 1231jljk23klj3

Can this be done using rex command ?
any help is appreciated !

0 Karma
1 Solution

vnravikumar
Champion

Hi @gowtham495

Try this

|makeresults | eval msg="jaskdjkasdkjas CR akjhdjhdjsdhCR 1231jljk23klj3 CR sagdiugsds 7126372 nklsdlkCR"| rex field=msg "(?P<output>.*)\sCR\s"

View solution in original post

0 Karma

nickhills
Ultra Champion

I have deviated slightly from your question, but I am surprised you dont want the value of the text between the 2nd CR and the 3rd CR?

in that case, the following example should work:

|makeresults | eval msg="jaskdjkasdkjas CR akjhdjhdjsdhCR 1231jljk23klj3 CR sagdiugsds 7126372 CR nklsdlkCR"| rex field=msg "((?P<output>.*?)\sCR\s){3}"

If you only wanted the 1st, or 2nd CR change the number in brackets

If my comment helps, please give it a thumbs up!
0 Karma

gowtham495
Path Finder

@nickhillscpl thanks for the query.
but it does not showing the desired output.
btw,i need the text between 2nd and 3rd CR too.

0 Karma

vnravikumar
Champion

Hi @gowtham495

Try this

|makeresults | eval msg="jaskdjkasdkjas CR akjhdjhdjsdhCR 1231jljk23klj3 CR sagdiugsds 7126372 nklsdlkCR"| rex field=msg "(?P<output>.*)\sCR\s"
0 Karma

gowtham495
Path Finder

thanks for the reply .
can you please explain how this takes care of "3rd CR "

0 Karma

vnravikumar
Champion

Hi @gowtham495

Try this
[Updated]

| makeresults 
 | eval msg="jaskdjkasdkjas CR akjhdjhdjsdhCR 1231jljk23klj3 CR sagdiugsds 7126372 CR nklsdlkCR" 
 | rex field=msg "(?<output>^((.*?)(CR)){3})" 
 | eval output=replace(output,"(\s*CR)$","")
0 Karma

vnravikumar
Champion

let me know any issues.

0 Karma

gowtham495
Path Finder

this one worked.. thanks !!!

0 Karma

vnravikumar
Champion

welcome 🙂

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!

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

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

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 ...