How to parse an output from exec node

I probably should know, but can't remember.

This is the flow:

[{"id":"21311374.ebd54c","type":"inject","z":"703c61ac.837e1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1800,"y":160,"wires":[["7f6fe149.ffcf7"]]},{"id":"2709158b.504aea","type":"debug","z":"703c61ac.837e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2040,"y":100,"wires":[]},{"id":"7f6fe149.ffcf7","type":"exec","z":"703c61ac.837e1","command":"cat /etc/os-release","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":1990,"y":160,"wires":[["2709158b.504aea","b3664456.b93568"],[],[]]},{"id":"b3664456.b93568","type":"function","z":"703c61ac.837e1","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1820,"y":240,"wires":[["41827515.dc15b4"]]},{"id":"41827515.dc15b4","type":"debug","z":"703c61ac.837e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2070,"y":240,"wires":[]}]

I get:

NAME="Ubuntu"VERSION="20.10 (Groovy Gorilla)"ID=ubuntuID_LIKE=debianPRETTY_NAME="Ubuntu 20.10"VERSION_ID="20.10"HOME_URL="https://www.ubuntu.com/"SUPPORT_URL="https://help.ubuntu.com/"BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"VERSION_CODENAME=groovyUBUNTU_CODENAME=groovy

I want to send/get the second line. The version line.

Thanks in advance.

Cant you just take the result as a string and make some clever splits?

That's what I am doing now actually.

I just wasn't sure there is/was an easier way.

I guess there are always many solutions (javascript in function node, JSONata, std nodes), why bother if it works? I would only consider alternatives if needed due to performance or maintainabilty reasons

let tst = 'NAME="Ubuntu"VERSION="20.10 (Groovy Gorilla)"ID=ubuntuID_LIKE=debianPRETTY_NAME="Ubuntu 20.10"VERSION_ID="20.10"HOME_URL="https://www.ubuntu.com/"SUPPORT_URL="https://help.ubuntu.com/"BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"VERSION_CODENAME=groovyUBUNTU_CODENAME=groovy'
tst = tst.split('"Ubuntu"')[1].split('"ID')[0];
node.warn(tst);

gives me

"VERSION="20.10 (Groovy Gorilla)"

This is what I had made:

[{"id":"7f6fe149.ffcf7","type":"exec","z":"aa48e352.9ad2f8","command":"cat /etc/os-release","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":390,"y":140,"wires":[["baae9c64.1cdbd"],[],[]]},{"id":"21311374.ebd54c","type":"inject","z":"aa48e352.9ad2f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":90,"wires":[["7f6fe149.ffcf7"]]},{"id":"baae9c64.1cdbd","type":"split","z":"aa48e352.9ad2f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":true,"addname":"","x":350,"y":190,"wires":[["d49fbae3.98499"]]},{"id":"d49fbae3.98499","type":"switch","z":"aa48e352.9ad2f8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"VERSION=","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":230,"wires":[["8f46b0d5.96dc7","a35b088a.5d2ba"]]},{"id":"8f46b0d5.96dc7","type":"debug","z":"aa48e352.9ad2f8","name":"Version","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":500,"y":270,"wires":[]}]
1 Like

Clever! Thats a good one just using std nodes. Simple to understand, simple to maintain. Also later in life when memory gets pushed back to the archive

If you want a standard JS object where you can use any other parts of the output...

[{"id":"7f6fe149.ffcf7","type":"exec","z":"d84ffa62.b7a148","command":"cat /etc/os-release","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":314,"y":256,"wires":[["baae9c64.1cdbd"],[],[]]},{"id":"21311374.ebd54c","type":"inject","z":"d84ffa62.b7a148","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":284,"y":192,"wires":[["7f6fe149.ffcf7"]]},{"id":"baae9c64.1cdbd","type":"split","z":"d84ffa62.b7a148","name":"split on \\n","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":508,"y":256,"wires":[["52f6b0fe.de942"]]},{"id":"8f46b0d5.96dc7","type":"debug","z":"d84ffa62.b7a148","name":"payload.VERSION","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload.VERSION","statusType":"msg","x":666,"y":320,"wires":[]},{"id":"d406e38d.c7a26","type":"join","z":"d84ffa62.b7a148","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":498,"y":320,"wires":[["8f46b0d5.96dc7"]]},{"id":"52f6b0fe.de942","type":"function","z":"d84ffa62.b7a148","name":"make topic+payload","func":"\nif ((msg.parts.index+1) == msg.parts.count) {\n    msg.complete = true;\n}\n\nif (msg.payload > \"\") {\n    var spl = msg.payload.split(\"=\");\n    msg.topic = spl[0];    //left part of payload is the KEY\n    msg.payload = spl[1];  //right part of payload is the VALUE\n    if(msg.payload.startsWith('\"') && msg.payload.endsWith('\"')) {\n        msg.payload = msg.payload.slice(1,-1); //remove quotes    \n    }    \n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":324,"y":320,"wires":[["d406e38d.c7a26"]]}]
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.