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!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...