# Extract data from a long string

**URL:** <https://discourse.nodered.org/t/extract-data-from-a-long-string/72531>\
**Category:** General\
**Created:** [21 December 2022 13:33 UTC](https://discourse.nodered.org/t/extract-data-from-a-long-string/72531 "2022-12-21T13:33:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)\
**Post date:** [21 December 2022 13:33 UTC](https://discourse.nodered.org/t/extract-data-from-a-long-string/72531/1 "2022-12-21T13:33:53Z")

</div>

Hi !

I am trying to check for memory usage on an OrangePi, and publish it via mqtt.

Using the 'free' command in a terminal window gives me this:

```
          total used free shared buff/cache available

```

Mem: 1023520 412268 107408 11524 503844 655444  
Swap: 511756 9216 502540

Using the same command in an exec node, I get the same result as a string:  
total used free shared buff/cache available  
Mem: 1023520 446516 67600 11528 509404 621192  
Swap: 511756 9216 502540

How to extract certaing value from this string?  
Like if I would like to extract the free memory?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 December 2022 13:52 UTC](https://discourse.nodered.org/t/extract-data-from-a-long-string/72531/2 "2022-12-21T13:52:40Z")

</div>

You can pull the whole output of **free** into Node-red and process it there, but I would just retrieve the single value from the command line.

For free memory I would exec this command **free | awk '/Mem/ {print $4}'**  
To explain the awk syntax:

- /Mem/ - select the line[s] which contain "Mem"
- {print $4} - Print the fourth field (seperated by white space)

On my Raspberry **free** gives

```auto
              total used free shared buff/cache available
Mem: 1916084 246080 1069332 27524 600672 1561896
Swap: 1916080 0 1916080

```

**free | awk '/Mem/ {print $4}'** gives the string

```auto
1069332

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 December 2022 14:18 UTC](https://discourse.nodered.org/t/extract-data-from-a-long-string/72531/3 "2022-12-21T14:18:39Z")

</div>

Or if you want all readings in an object you could run the string through a function node using

```auto
let lines = msg.payload.split(/\n/);
let names = lines[0].split(/\s+/).slice(1);
let mem = lines[1].split(/\s+/).slice(1);
let swap = lines[2].split(/\s+/).slice(1);
msg.payload = {};
names.forEach((str, index) => {
    if(mem[index]) msg.payload["mem_" + str] = Number(mem[index]);
    if(swap[index]) msg.payload["swap_" + str] = Number(swap[index]);
})
return msg;

```

Output

```auto
{ 
  "mem_total":3838148,
  "swap_total":511996,
  "mem_used":2890444,
  "swap_used":343064,
  "mem_free":162616,
  "swap_free":168932,
  "mem_shared":4452,
  "mem_buff/cache":785088,
  "mem_available":719420
}

```

---

<div class="post-metadata">

**Author:** ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)\
**Post date:** [21 December 2022 15:38 UTC](https://discourse.nodered.org/t/extract-data-from-a-long-string/72531/4 "2022-12-21T15:38:42Z")

</div>

Wow, that works perfectly on the OrangiPi also. Super, thank for your help !!!

---

<div class="post-metadata">

**Author:** ![mzsolnay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mzsolnay/32/40308_2.png) [@mzsolnay](https://discourse.nodered.org/u/mzsolnay)\
**Post date:** [21 December 2022 15:40 UTC](https://discourse.nodered.org/t/extract-data-from-a-long-string/72531/5 "2022-12-21T15:40:42Z")

</div>

I admire anyone, who can just spit out codes lik this 🙂

Thank you for your help !

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 December 2022 15:56 UTC](https://discourse.nodered.org/t/extract-data-from-a-long-string/72531/6 "2022-12-21T15:56:05Z")

</div>

Nice, though I can't tell by inspection what the slice(1) bit does.  
By experiment: it throws away array[0] ( "", "Mem:" or "Swap:")

I am slightly disappointed there were no silicon pixies in this answer! 🤣

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 February 2023 15:56 UTC](https://discourse.nodered.org/t/extract-data-from-a-long-string/72531/7 "2023-02-19T15:56:14Z")

</div>

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