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?
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.
Hi
Starting in 24.10, NQE has regex matching functions. This gives you another way to solve the above problem, like this:
testDeviceNames = ="aaa_bbb_ccc", "1_22222_33"];
foreach deviceName in testDeviceNames
let match = match(deviceName, re`(?<part1>\w+)_(?<part2>\w+)_(?<part3>\w+)`)
select {
device: deviceName,
part1: match?.part1,
part2: match?.part2,
part3: match?.part3
}
The nice thing here is that you do not need to first replace underscores with space characters. A minor improvement here, but may be helpful on more complex examples as well.
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.