It needs to be debugged (not by me!) but here's how to approach the problem: write a recursive function that just descends every time it hits "(", build up an array indexed by the current depth of the calls at that time, and print that array contents when you hit the last ")" in the string:
$ cat tst.awk
BEGIN { RS="[)]\\s*\"\\s*" }
function descend(tail) {
if ( ++depth == 30 ) {
print "ERROR: went too deep" | "cat>&2"
exit 1
}
while ( match(tail,/([^()]+)([()])(.*)/,a) ) {
val[depth] = gensub(/^\s+|\s+$/,"","g",a[1])
if ( a[2] == "(" ) {
descend(a[3])
}
else {
for (i=1; i<=depth; i++) {
printf "%s,", val[i]
}
print ""
}
tail = a[3]
}
--depth
}
{ sub(/^[^"]+"[(]/,""); descend($0) }
.
$ awk -f tst.awk file
channel,1,saturation,14,
channel,1,saturation,,
channel,1,saturation,,2,saturation,41,
channel,1,saturation,,2,saturation,,
channel,1,saturation,,2,saturation,,3,saturation,25,
channel,1,saturation,,2,saturation,,3,saturation,,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,27,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,5,saturation,33,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,5,saturation,,ssid,0,ssid,TestingAlpha,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,5,saturation,,ssid,0,ssid,,rssi,5,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,5,saturation,,ssid,0,ssid,,rssi,,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,5,saturation,,ssid,0,ssid,,rssi,,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,5,saturation,,ssid,0,ssid,,rssi,,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,5,saturation,,ssid,0,ssid,,rssi,,6,saturation,100,
channel,1,saturation,,2,saturation,,3,saturation,,4,saturation,,5,saturation,,ssid,0,ssid,,rssi,,6,saturation,,ssid,0,ssid,TestingBravo,
ERROR: went too deep
The above uses GNU awk for multi-char RS and gensub().
I do really like the idea of converting it to JSON and then using jq
on it instead though, just not something I'm familiar enough with JSON or jq to tackle.