Splunk Search

how to remove start and last character from field value in splunk using single command

n4niyaz
Explorer

how to remove start and last character from field value please find the example below

Example

test=road-car
test=a_road_car-ts
test=road-bus
test=a_road_bus-ts

if i execute stats value result must be road-car=2 and road-bus=2

Tags (1)
0 Karma

woodcock
Esteemed Legend

Like this (the secret sauce is in the sed command; you may need to adjust the 2 to something different.):

| makeresults|eval test="road-car a_road_car-ts road-bus a_road_bus-ts"
| makemv test
| mvexpand test
| rex field=test mode=sed "s/_/-/g s/^[^-]{1,2}[-]// s/[-][^-]{1,2}$//"
| stats count BY test
0 Karma

TISKAR
Builder

Can You try this:

| makeresults 
| eval test="road-car,a_road_car-ts,road-bus,a_road_bus-ts"
| makemv delim="," test
| mvexpand test
| rex field=test "(?<test>road[-_][^-]*)"
| replace *_* with *-* IN test

In your request:

<Your_searcg> |  rex field=test "(?<test>road[-_][^-]*)" | replace *_* with *-* IN test
0 Karma

richgalloway
SplunkTrust
SplunkTrust

The title of your post implies removal of a single character from each end of the field, however, your examples remove multiple characters. There's also a mix of hyphens and underscores. Using your examples as a template, this rex command should do the job for you.

... | rex field=test "(?:\w_)?(?<foo>[-\w]+)(?:-\w\w)?" | stats count by foo | ...
---
If this reply helps you, Karma would be appreciated.
0 Karma

niketn
Legend

Can you try the following:

 <yourSearch>
| stats count(eval(match(test,"car"))) as road-car count(eval(match(test,"bus"))) as road-bus

Following is a run anywhere search based on sample data provided.

| makeresults
| eval data="test=road-car;test=a_road_car-ts;test=road-bus;test=a_road_bus-ts"
| makemv data delim=";"
| mvexpand data
| rename data as _raw
| KV
| stats count(eval(match(test,"car"))) as road-car count(eval(match(test,"bus"))) as road-bus
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
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 ...