# Select most recent added x rows from a CSV-file

**URL:** <https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728>\
**Category:** General\
**Created:** [16 May 2021 16:22 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728 "2021-05-16T16:22:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [16 May 2021 16:22 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/1 "2021-05-16T16:22:17Z")

</div>

A comma separated CSV file consists of only two columns (_datetime, pressure_) but many rows. Does anyone know whether it is possible to read only the x most recent measurements from that CSV file?

Any help is welcome!

---

<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:** [16 May 2021 16:49 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/2 "2021-05-16T16:49:34Z")

</div>

Presuming the last 2 lines are the latest entries, then you would read the csv file, then output the last 2 array values.  
e.g.

```auto
[{"id":"965ef7ad.16d87","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":800,"wires":[["d16bafd0.71ee88"]]},{"id":"d16bafd0.71ee88","type":"template","z":"9b3f9f31.c45298","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"date,pressure\n12/06/2020,45\n13/06/2020,47\n15/06/2020,50","output":"str","x":110,"y":860,"wires":[["21a21fc0.7d62"]]},{"id":"21a21fc0.7d62","type":"csv","z":"9b3f9f31.c45298","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":260,"y":860,"wires":[["a82f5588.69ba6"]]},{"id":"a82f5588.69ba6","type":"change","z":"9b3f9f31.c45298","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[[-2,-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":860,"wires":[["a4e8e83.9c24918"]]},{"id":"a4e8e83.9c24918","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":860,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [16 May 2021 17:15 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/3 "2021-05-16T17:15:41Z")

</div>

Thanks for your help, this works well to get the last rows out of the CSV file.

But now I see I was not clear with my question, sorry. It is not the intention to put those last rows in an array, I want to generate a new (different) CSV file with it. Is that also possible?

---

<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:** [16 May 2021 17:22 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/4 "2021-05-16T17:22:07Z")

</div>

Then feed the array back through a cvs node and it will turn the 2 element array back into a csv.

e.g.

```auto
[{"id":"cc9edeb9.ff689","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":400,"wires":[["70ac867e.3b19d"]]},{"id":"70ac867e.3b19d","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"date,pressure\n12/06/2020,45\n13/06/2020,47\n15/06/2020,50","output":"str","x":210,"y":460,"wires":[["892ca195.f46cb"]]},{"id":"892ca195.f46cb","type":"csv","z":"c74669a0.6a34f8","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":360,"y":460,"wires":[["dab29fbd.3cc03"]]},{"id":"dab29fbd.3cc03","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[[-2,-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":460,"wires":[["9d223cc0.c2a008"]]},{"id":"9d223cc0.c2a008","type":"csv","z":"c74669a0.6a34f8","name":"","sep":",","hdrin":true,"hdrout":"all","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":670,"y":500,"wires":[["14ddc802.a51cb8"]]},{"id":"14ddc802.a51cb8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":460,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [16 May 2021 17:45 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/5 "2021-05-16T17:45:23Z")

</div>

Exactly what I meant and it works well thanks for your help! It is a pleasure to work with Node-RED because there are always people on this forum who want to help others 👍🏻

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [17 May 2021 12:55 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/6 "2021-05-17T12:55:52Z")

</div>

Hi E1cid. Unfortunately, things are not going well yet, maybe this is due to the somewhat strange datetime format in my CSV file. That is generated externally and I cannot change it. I have now put that CSV file in the template (see flow), can you see again how I can save the last 20 rows in another CSV file?

```auto
[{"id":"c074fcd7.0f257","type":"template","z":"f88f5df4.1e07f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"datetime,pressure\n17-05-2021 08:29,998\n17-05-2021 08:45,999\n17-05-2021 09:01,1000\n17-05-2021 09:15,1000\n17-05-2021 09:30,1001\n17-05-2021 09:45,1001\n17-05-2021 10:00,1001\n17-05-2021 10:15,1001\n17-05-2021 10:30,1001\n17-05-2021 10:45,1001\n17-05-2021 11:00,1001\n17-05-2021 11:15,1001\n17-05-2021 11:30,1001\n17-05-2021 11:45,1001\n17-05-2021 12:00,1001\n17-05-2021 12:09,1001\n17-05-2021 12:10,1001\n17-05-2021 12:12,1001\n17-05-2021 12:31,1001\n17-05-2021 12:46,1001\n17-05-2021 13:16,1002\n17-05-2021 13:31,1002\n17-05-2021 13:46,1002\n17-05-2021 14:01,1002\n17-05-2021 14:15,1001\n17-05-2021 14:21,1003\n17-05-2021 14:36,1003\n17-05-2021 14:50,1002\n17-05-2021 15:06,1003\n17-05-2021 15:21,1004\n\n","output":"str","x":240,"y":240,"wires":[["2527816e.78f46e"]]}]

```

---

<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:** [17 May 2021 13:12 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/7 "2021-05-17T13:12:00Z")

</div>

You neede to change the jsonata expression in the change node  
[[-2,-1]] should be [[-20..-1]] the .. denotes a range.

```auto
[{"id":"cc9edeb9.ff689","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":390,"y":340,"wires":[["c074fcd7.0f257"]]},{"id":"c074fcd7.0f257","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"datetime,pressure\n17-05-2021 08:29,998\n17-05-2021 08:45,999\n17-05-2021 09:01,1000\n17-05-2021 09:15,1000\n17-05-2021 09:30,1001\n17-05-2021 09:45,1001\n17-05-2021 10:00,1001\n17-05-2021 10:15,1001\n17-05-2021 10:30,1001\n17-05-2021 10:45,1001\n17-05-2021 11:00,1001\n17-05-2021 11:15,1001\n17-05-2021 11:30,1001\n17-05-2021 11:45,1001\n17-05-2021 12:00,1001\n17-05-2021 12:09,1001\n17-05-2021 12:10,1001\n17-05-2021 12:12,1001\n17-05-2021 12:31,1001\n17-05-2021 12:46,1001\n17-05-2021 13:16,1002\n17-05-2021 13:31,1002\n17-05-2021 13:46,1002\n17-05-2021 14:01,1002\n17-05-2021 14:15,1001\n17-05-2021 14:21,1003\n17-05-2021 14:36,1003\n17-05-2021 14:50,1002\n17-05-2021 15:06,1003\n17-05-2021 15:21,1004\n\n","output":"str","x":210,"y":460,"wires":[["892ca195.f46cb"]]},{"id":"892ca195.f46cb","type":"csv","z":"c74669a0.6a34f8","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":420,"y":400,"wires":[["dab29fbd.3cc03"]]},{"id":"dab29fbd.3cc03","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[[-20..-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":400,"wires":[["9d223cc0.c2a008"]]},{"id":"9d223cc0.c2a008","type":"csv","z":"c74669a0.6a34f8","name":"","sep":",","hdrin":true,"hdrout":"all","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":730,"y":440,"wires":[["14ddc802.a51cb8"]]},{"id":"14ddc802.a51cb8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":400,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [17 May 2021 13:41 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/8 "2021-05-17T13:41:08Z")

</div>

The two dots make the difference, I have been helped fantastic again. Thanks!

---

<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:** [31 May 2021 13:41 UTC](https://discourse.nodered.org/t/select-most-recent-added-x-rows-from-a-csv-file/45728/9 "2021-05-31T13:41:39Z")

</div>

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