Splunk Search

Extract a string

ashu_g50
Path Finder

Hi we log the data in splunk as below

2013-01-07 09:37:35.556935 client="rmf-rm3d"|jobRunId="1504312"|salesforceId="null"|JobDurationSecs="183.561"|description="null"|processid="bzghn-3i8-4hg7"|schedulerId="id.20130107_093409.rmf-rm3d.rm3d.1375765"|numberOfCompletedSteps="9"|positionsProxied="27"|client="rmf-rm3d"|reportsCount="0"|portfoliosSplit="1"|parts="Import"|reportsSize="0"|positionsAttempted="520"|jobName="rm3d"|autoSysID="1375765"|............

we have a field called controlFiles="m_ifs_swr_d_1.20130103.cntl.txt"

I have the query to extract the control file as its a variable but what I want is just the 1st part
m_ifs_swr_d_1 and not 20130103.cntl.txt.

now the 1st and the 2nd part are not of fixed length but can be maximum 3
what I mean is the format for the control file is

....

now filename can be part1 or part2 or part3 usually its two parts. but the only constant in the format is .cntl even the file type may or may not be present (usually its not) so I want to extract anything thats before the .cntl

how can I achieve this.

Tags (1)
0 Karma
1 Solution

kristian_kolb
Ultra Champion

If you only want the text up to, but not including the first dot (.) - which seems to be the pattern here - you can do it inline through a rex statement.

your search | rex field = controlFiles "(?<ctrlfile>[^.]+)\." | the rest of your search

That gives you a new field called ctrlfile that only contains the first part.

If you want the field to contain everything (including dots) but date.cntl.txt, you could try:

your search | rex field = controlFiles "(?<ctrlfile>.*)\.\d{10}\.cntl\.txt" | the rest of your search

You probably understand how to take if from here if these patterns are not correct. If not, ask for a clarification.

Hope this helps,

Kristian

View solution in original post

gfuente
Motivator

Hello

Here you have another option:
Regular Expression:
controlFiles="(?\w*).(?\w+)?.?(?\w+)?.?\d+.cntl.\w+"

Splunk Command:

... | rex field = controlFiles "controlFiles="(?\w*).(?\w+)?.?(?\w+)?.?\d+.cntl.\w+"" | ...

I tried it with RegExRX and it works for me

Try it and let me know if it doesn´t work for you

Regards

0 Karma

kristian_kolb
Ultra Champion

If you only want the text up to, but not including the first dot (.) - which seems to be the pattern here - you can do it inline through a rex statement.

your search | rex field = controlFiles "(?<ctrlfile>[^.]+)\." | the rest of your search

That gives you a new field called ctrlfile that only contains the first part.

If you want the field to contain everything (including dots) but date.cntl.txt, you could try:

your search | rex field = controlFiles "(?<ctrlfile>.*)\.\d{10}\.cntl\.txt" | the rest of your search

You probably understand how to take if from here if these patterns are not correct. If not, ask for a clarification.

Hope this helps,

Kristian

Get Updates on the Splunk Community!

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...