# Help with stripping a message to what I want

**URL:** <https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993>\
**Category:** General\
**Created:** [3 September 2023 06:36 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993 "2023-09-03T06:36:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 06:36 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/1 "2023-09-03T06:36:37Z")

</div>

(If I don't ask, I will get stuck. If I ask I may work it out myself. But if I don't - as said: I'll get stuck)

Here goes:

`exec` node.  
`ntpdate -q 192.168.17.39`

I get:

```auto
server 192.168.17.39, stratum 3, offset -0.000113, delay 0.02623
 3 Sep 16:33:26 ntpdate[5770]: adjust time server 192.168.17.39 offset -0.000113 sec

```

I want the `3 Sep 16:33:26` part.

Help, I am not that good with the `string` node.

---

<div class="post-metadata">

**Author:** ![MDAR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdar/32/10700_2.png) [@MDAR](https://discourse.nodered.org/u/MDAR)\
**Post date:** [3 September 2023 06:45 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/2 "2023-09-03T06:45:35Z")

</div>

At first glance.

It seems there are two delimiters bring used in that string.

A comma the separate the tags and values

And a space to separate the value from the tag.

I'm a long way from being any imagination, so I have to assume there is a lot nicer way of doing it that I do.

I would create a loop to spilt tags & values and put them into an array.

Then call whichever part of the array you want later.

* * *

I'm not using my PC right now, when I fire it up later I'll paste a bit of sample code that I used recently for working with a `Black Magic Design, HyperDeck ` that sends its replies in a simple text string, with `New Line /n` delimiters

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 06:47 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/3 "2023-09-03T06:47:39Z")

</div>

(as I said.... If I didn't ask.)

This is what I concocted in the mean time:

```auto
[{"id":"71269203f11f4531","type":"inject","z":"650fc4d9.b1c234","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":3280,"wires":[["1d429c3dd01e6174"]]},{"id":"7e483af4eca8f8d4","type":"exec","z":"650fc4d9.b1c234","command":"ntpdate -q 192.168.17.39","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":470,"y":3380,"wires":[["ee262f99dde4430f","644be43955c648da","f2d01e650a1f911d"],[],[]]},{"id":"ee262f99dde4430f","type":"string","z":"650fc4d9.b1c234","name":"","methods":[{"name":"delRightMost","params":[{"type":"str","value":"adjust"}]},{"name":"delLeftMost","params":[{"type":"str","value":"\\n"}]},{"name":"delRightMost","params":[{"type":"str","value":"ntp"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":730,"y":3370,"wires":[["f46bea53719f1010","d9722e5510ae34eb"]]},{"id":"d9722e5510ae34eb","type":"debug","z":"650fc4d9.b1c234","name":"NTP time","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":870,"y":3410,"wires":[]}]

```

Thoughts?

---

<div class="post-metadata">

**Author:** ![MDAR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdar/32/10700_2.png) [@MDAR](https://discourse.nodered.org/u/MDAR)\
**Post date:** [3 September 2023 06:49 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/4 "2023-09-03T06:49:07Z")

</div>

Quick question

Is this

```auto
server 192.168.17.39, stratum 3, offset -0.000113, delay 0.02623
 3 Sep 16:33:26 ntpdate[5770]: adjust time server 192.168.17.39 offset -0.000113 sec

```

the exact string that gets returned or are there any `/n` in there too?

---

<div class="post-metadata">

**Author:** ![MDAR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdar/32/10700_2.png) [@MDAR](https://discourse.nodered.org/u/MDAR)\
**Post date:** [3 September 2023 06:52 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/5 "2023-09-03T06:52:23Z")

</div>

It's not even 8am here and I haven't had a coffee yet....

I'll take a look when I fire up a PC 🤓

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 06:53 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/6 "2023-09-03T06:53:24Z")

</div>

Oh, sorry. You don't have access to NR just now.

Screen shot.

😉

 ![Screenshot from 2023-09-03 16-52-05](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/5971569f7058536de0ea10bb9e0ec1de218c3dab.png)

Is that better?  
(Again: sorry.)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [3 September 2023 06:54 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/7 "2023-09-03T06:54:36Z")

</div>

Quickly splitting by space, filtering out the ones you are after and re-joining

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c804a2af60551fa82b60b5db61533d0acb7eb58.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c01c0b4798310f3075dfdf98cc4025583b1e904e.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/595df190dc0a7748419c790f9dec6c4c91f2cbfe.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 06:55 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/8 "2023-09-03T06:55:43Z")

</div>

That's another way I guess.

Thanks.

Thoughts on the way I posted?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [3 September 2023 06:56 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/9 "2023-09-03T06:56:56Z")

</div>

Works as well - probably more robust than my method 🙂

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 06:57 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/10 "2023-09-03T06:57:48Z")

</div>

I'm not sure as I haven't really _tested it_ if there is no NTP connection.

(Ooops.)

I guess all I need to do is put in a wrong IP address and see what comes out. 😉

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 September 2023 07:02 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/11 "2023-09-03T07:02:15Z")

</div>

Regex is ideal for these.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a054c90bdb9a701332de3c7d2469ed219d345b12.png)

```javascript
/*
  FUNCTION NOTES:
  \d+ means 1 or more numbers (d for digits)
  \w+ any word char (a-Z0-9)

  therefore \d+ \w+ \d+:\d+:\d+ 
  matches 10 Jan 11:22:33
  matches 3 Sep 16:33:26

  You can be more strict if required e.g.
  * \w\w\w or \w{3} means get 3 WORD chars exacly
  * \d\d or \d{2} means get 2 numbers exaclty

  // see playground here: https://regex101.com/r/i1aXg6/1
*/

const result = /(\d+ \w+ \d+:\d+:\d+)/gm.exec(msg.payload)
if (result && result.length) {
    msg.payload = result[1]
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 07:05 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/12 "2023-09-03T07:05:14Z")

</div>

I like that Steve as I can then add a message that is sent if the `if` fails - which I hadn't got to in this bit.  
I'll give it a go now.

---

<div class="post-metadata">

**Author:** ![MDAR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdar/32/10700_2.png) [@MDAR](https://discourse.nodered.org/u/MDAR)\
**Post date:** [3 September 2023 07:15 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/13 "2023-09-03T07:15:48Z")

</div>

> [@Trying\_to\_learn](#):
>
> You don't have access to NR just now

I've only just got access to a coffee machine 🤣

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 07:16 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/14 "2023-09-03T07:16:21Z")

</div>

🙂

I hear you.

Nothing beats a good coffee.

---

<div class="post-metadata">

**Author:** ![MDAR](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mdar/32/10700_2.png) [@MDAR](https://discourse.nodered.org/u/MDAR)\
**Post date:** [3 September 2023 07:24 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/15 "2023-09-03T07:24:50Z")

</div>

@Trying_to_learn

Arrr, yes, I see in your screen grab that the returned string **does** include new lines, which impacts the method I would have used.

```auto
server 192.168.17.39, stratum 3, offset -0.000113, delay 0.02623/n
 3 Sep 16:33:26 ntpdate[5770]: adjust time server 192.168.17.39 offset -0.000113 sec/n

```

Steve's method works perfectly if you only ever want the date or extract section.

My approach is always to "capture all", even if I discard it ms later.

Only because my mind curses me by thinking, "ohhh, I could use that other variable"

As seen in a solution I provided for a "[Riedel Bolero](https://www.riedel.net/en/products-solutions/intercom/bolero-wireless-intercom)" radio pack station loading Dashboard

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 07:26 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/16 "2023-09-03T07:26:53Z")

</div>

This is some code I am writing for my _network tester_ flow.

Tests network availability, DNS, blocks and NTP.

So I needed to add this bit to get the NTP part working.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 07:41 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/17 "2023-09-03T07:41:47Z")

</div>

(Sorry @Steve-Mcl)

I just tested that code to _braking it_.

This is what I get if it can't get to the NTP specified.

```auto
ntpdate -q 192.168.17.39
server 192.168.17.39, stratum 0, offset 0.000000, delay 0.00000
 3 Sep 17:39:45 ntpdate[8123]: no server suitable for synchronization found

```

So I need to make sure that end bit is not there.  
If it is: Send a different message.

SORRY.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 September 2023 07:45 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/18 "2023-09-03T07:45:47Z")

</div>

If you wish to avoid bugs, I strongly recommend you also extract the IP and check it against what you expected - this will avoid any ambiguity about the NTP response.

Here is an update that:

1. gets the IP and time string (you should check the IP is what you expected)
2. returns an object - because objects are future proof (returning a string means lots of re-work if something changes)
3. has a 2nd output if not found (for error processing)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20fa9142cf66dfd25ee359d4fc5ace7872a255df.png)

```auto
const result = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?(\d+ \w{3} \d+:\d+:\d+)/s.exec(msg.payload)
if (result && result.length == 3) {
    msg.payload = {
      ip: result[1],
      time: result[2]
    }
  return [msg, null]
}

msg.payload = {
  error: 'ip and time strings not found'
}
return [null, msg]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 September 2023 07:47 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/19 "2023-09-03T07:47:51Z")

</div>

Yeah, sorry Steve.

I maybe should have declared that at the start maybe.

This is all for local testing my new network.

I need to make sure the NTP is working - which it isn't just now.  
Before this I didn't know that.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 September 2023 07:48 UTC](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993/20 "2023-09-03T07:48:35Z")

</div>

> [@Trying\_to\_learn](#):
>
> So I need to make sure that end bit is not there.

A simple pre-check will solve that (e.g. `if(msg.payload.contains('no server suitable for synchronization found')) { ... }` BUT - I suspect the better solution is to check the other 2 outputs of the exec node. I would hope it returns an error code - that is more definitive than a string search

In short, on the exec node, what do you get out of pin2 and pin3 for :-

- a successful execution?
- an unsuccessful execution?

identify the difference & use that BEFORE running the regex function.

[Next page](https://discourse.nodered.org/t/help-with-stripping-a-message-to-what-i-want/80993.md?page=2)
