Cut the beginning and ending of a tag value

Hey,

I’m calling all the possible tag values with the following query:

import “influxdata/influxdb/v1”

v1.tagValues(

bucket: "MyBucket",

tag: "Generic_Name",

predicate: (r) => true,

start: -1d

)

The tag values returned look like this:

“Machine_Number/Name1/Name2/Sensortype/Unit”

What i want to achive is that i cut out the Sensortype and Unit.
I tried to achive this with the strings.trimright() function

But i was only able to cut out a specificly defined text. The Problem is that every unit and every sensortype is called differently.

Is there a way to make this trimming funktion dynamical so that it trims everything to the second / from the right?

Hello @Patse,
Is the front end of your text the same?
You could use regex instead.

I like to use this tester

I found a solution but I don’t like it:

import "array"
import "strings"

word = "Machine_Number/Name1/Name2/Sensortype/Unit"

array.from(rows: [{_time: now(), _value: word}])

|> map(fn: (r) => ({ r with suffix: strings.trimPrefix(v: r._value, prefix: "Machine_Number/Name1/Name2/")

}))

|> map(fn: (r) => ({ r with prefix: strings.trimSuffix(v: r._value, suffix: r.suffix)

}))

I’m sharing with the Flux team too in case there are any other solutions I’m not aware of.

Hey, the front and the back end of my tag values are different for each machine and sensor type.
The only thing that is identical is the number of “/” in between each part of my tag value.
That’s why i hoped that there is a possibility to say trim until the second “/” from the right.

Hello @Patse,
I think you can say that with regex. I’m just not sure how.

@Anaisdg

I was trying to do something similar, cutting the date off a string x-y-z_20220101 and the search led me to this post.

This doesn’t solve the original problem, but might help anyone else trying to cut the last part of a string containing delimiters, where the string is variable in length:

import "array"
import "strings"

word = "Machine_Number/Name1/Name2/Sensortype/Unit"

array.from(rows: [{_time: now(), _value: word}])
 |> map(fn: (r) => ({
      r with
      start: strings.lastIndex(v: r._value, substr: "/") + 1,
      end: strings.strlen(v: r._value),
    })
  )
  |> map(fn: (r) => ({_time:r._time, _value: strings.substring(v: r._value, start: r.start, end: r.end) }))
1 Like