Skip to main content
Solved

Split the output string value

  • 28 August 2024
  • 1 reply
  • 35 views

foreach device in network.devices

select {

  deviceName: device.name

}

For the above NQE, if I get the device names in the format “aaa_bbb_ccc”, is there a way to split the output based on the string “_” and have three different fields?

You can utilize the patternMatch function to split the device name string into different columns as follows:

parts(deviceName) =
patternMatch(replace(deviceName, "_", " "), `{part1:string} {part2:string} {part3:string}`);

foreach device in network.devices

select {
device: device.name,
device1: parts(device.name)?.part1,
device2: parts(device.name)?.part2,
device3: parts(device.name)?.part3,
}

Underscores in the string are replaced by spaces, since spaces are used by the patternMatch function to parse the string.

Note the (?) after the parts(device.name) function is called. This allows for the condition where the output of the function is null.


Reply