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!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...