Splunk Search

How to write an expression that extracts only the first three letters of a string?

mayank101
New Member

I have a different string named:

1.  GBP:BOOT1STSUNMONTH_MAINT2
2.  AMP:BOOT1STSATMONTH_MAINT4
3.  AMP:USFIMBSWEEKENDSVRSTOP_REBOOT_POSTCHECK_Box

I want to write an expression that extracts only the first three letter of the string and then store AMP, AMS into 1 entity named as AS, other GBP into another entity named EU.
Can you please help me in writing an expression for this, I am quite new to Splunk.

0 Karma

vnravikumar
Champion

Hi

Try this

| makeresults 
| eval test="GBP:BOOT1STSUNMONTH_MAINT2,AMP:BOOT1STSATMONTH_MAINT4,AMP:USFIMBSWEEKENDSVRSTOP_REBOOT_POSTCHECK_Box" 
| makemv delim="," test 
| mvexpand test 
| rex field=test "(?P<result>^.{0,3})" 
| eval AS = if(result=="AMP" OR result == "AMS", result,"") 
| eval EU = if(result=="GBP", result,"")
0 Karma

mayank101
New Member

Hi Ravi,
Thanks for the prompt response but the problem is there are many such strings .Its not just these. There are around 1000 of strings that start with AMP or GBP under the field name called entity.I cant incorporate all the strings in the eval test function. Could you help in this case.

0 Karma

vnravikumar
Champion

For sample purpose, I had mentioned those string in test, replace the same with the actual field in the index

0 Karma

mayank101
New Member

index="nmr-eis-gl-sysm-amber"
(publisher="gl_autosys_az" entity="*boot" ).My initial query is this..so you want me to replace those string with index ("nmr-eis-gl-sysm-amber" ).So I'm query I will be using index two times

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...

Network to App: Observability Unlocked [May & June Series]

In today’s digital landscape, your environment is no longer confined to the data center. It spans complex ...

SPL2 Deep Dives, AppDynamics Integrations, SAML Made Simple and Much More on Splunk ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...