# Lists. Need a bit of help

**URL:** <https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157>\
**Category:** General\
**Created:** [8 August 2019 21:30 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157 "2019-08-08T21:30:24Z")\
**Posts on this page:** 15\
**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:** [8 August 2019 21:30 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/1 "2019-08-08T21:30:24Z")

</div>

I'm kind of stuck with a problem. (Apart from the obvious)

Here is part of the flow and the list generation:

```auto
[{"id":"1deb29d5.dfb98e","type":"ui_button","z":"a3bd09a.b852978","name":"Generate reboot history list","group":"9630e6fb.2f4208","order":1,"width":"4","height":"1","passthru":false,"label":"Generate","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"date","topic":"","x":370,"y":150,"wires":[["88ec09d0.6e1c28"]]},{"id":"88ec09d0.6e1c28","type":"exec","z":"a3bd09a.b852978","command":"cat /media/pi/9020-9C27/logs/*","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Cat","x":470,"y":210,"wires":[["17917965.d43657"],[],[]]},{"id":"17917965.d43657","type":"split","z":"a3bd09a.b852978","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":630,"y":200,"wires":[["f8e85ffb.27a12"]]},{"id":"f8e85ffb.27a12","type":"switch","z":"a3bd09a.b852978","name":"","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":630,"y":250,"wires":[["8d28b379.4fc7b","80f6579c.fcf2e8"]]},{"id":"80f6579c.fcf2e8","type":"ui_template","z":"a3bd09a.b852978","group":"9630e6fb.2f4208","name":"Reboot history","order":0,"width":"8","height":"6","format":"<table id=\"table\" border=\"1\">\n <tr>\n <th>Reboot times</th> \n </tr>\n <tbody>\n <tr ng-repeat=\"row in msg.payload track by $index\">\n <td class=\"text\" >{{row}}</td>\n </tr>\n </tbody>\n</table>\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":840,"y":380,"wires":[[]]},{"id":"9630e6fb.2f4208","type":"ui_group","z":"","name":"Reboot history","tab":"22b3809f.04cca","order":1,"disp":true,"width":"8","collapse":false},{"id":"22b3809f.04cca","type":"ui_tab","z":"","name":"History","icon":"dashboard","order":6,"disabled":false,"hidden":false}]

```

The _problem_ is that the list is backwards to read.  
I'm wanting the most recent at the top, rather than the bottom.

As it is, when the list is presented the scroll is at the top of the window and I have to scroll down to see the latest _line_.

Though that is how things usually are laid out (like in books) it would be nicer if the latest was at the top.

I have another version which does similar and it too is the wrong way around.

I could reverse list the file with `tac`, rather than `cat` but from memory it doesn't fix the problem.

I shall check again later today but I am just putting the question out there because if I am wrong in thinking that `tac` will work, it is better the question is out there now rather than in 8 hours time.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 August 2019 23:00 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/2 "2019-08-08T23:00:56Z")

</div>

It would be easier if you shared an example of the data as well. It isn't really feasible for us to paste in everyone's code.

Without looking at the data, I'm rather guessing but I would normally do this in Javascript by splitting the text into an array then sorting the array.

---

<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:** [9 August 2019 03:10 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/3 "2019-08-09T03:10:17Z")

</div>

Sorry.

Example file:

```auto
2019-7-16 22:10:12 Tue Jul 16 2019 22:10:12 GMT+1000 (AEST) >> Backup << 
2019-7-17 14:15:40 Wed Jul 17 2019 14:15:40 GMT+1000 (AEST) >> Backup << 
2019-7-17 17:45:48 Wed Jul 17 2019 17:45:48 GMT+1000 (AEST) >> Backup << 
2019-7-17 20:40:33 Wed Jul 17 2019 20:40:33 GMT+1000 (AEST) >> Reboot << 
2019-7-17 20:43:33 GO
2019-7-20 07:40:32 Sat Jul 20 2019 07:40:32 GMT+1000 (AEST) >> Reboot << 
2019-7-20 07:43:30 GO
2019-7-21 16:32:10 GO
2019-7-22 06:26:09 GO
2019-7-22 06:36:27 GO
2019-7-23 08:31:24 Tue Jul 23 2019 08:31:24 GMT+1000 (AEST) >> Backup << 
2019-7-28 23:33:20 Sun Jul 28 2019 23:33:20 GMT+1000 (AEST) >> Test1 << 
2019-7-30 10:38:23 Tue Jul 30 2019 10:38:23 GMT+1000 (AEST) >> Backup << 
2019-8-1 07:14:15 GO
2019-8-6 09:26:19 GO
2019-8-6 13:26:22 GO
2019-8-6 14:28:45 GO
2019-8-6 14:32:41 GO
2019-8-6 21:09:31 GO
2019-8-6 23:01:08 Tue Aug 06 2019 23:01:08 GMT+1000 (AEST) >> Backup << 
2019-8-7 08:34:57 Wed Aug 07 2019 08:34:57 GMT+1000 (AEST) >> Shutdown << 
2019-8-7 16:43:05 Wed Aug 07 2019 16:43:05 GMT+1000 (AEST) >> Shutdown << 

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 August 2019 08:49 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/4 "2019-08-09T08:49:49Z")

</div>

No problem.

Hmm, bit of a problem there. The dates in that log are not sortable. They need to have leading zero's in the month and date so that they can be sorted correctly.

Thankfully though, JavaScript as an array reverse function:

> **[Array.prototype.reverse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)**
>
> The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

The downside of this compared to doing it when you run the command would be memory use. You may need to check how the `tac` command works to see whether it is more efficient than JavaScript's reverse. If the log isn't that large, it likely won't make any difference.

---

<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:** [9 August 2019 21:35 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/5 "2019-08-09T21:35:37Z")

</div>

Thanks.

Ok, bit more information for you.

I get that the names in the file are _confusing_ for sorting.  
It isn't I want to sort them. That isn't needed. The lines are added in time order to the file.

I tried it with `tac` rather than `cat` and though it works...... Because the list on the screen is limited in size: the latest events are pushed off the top of the list by the older ones at the bottom, which isn't desirable.

So basically they are sorted.

But when I put them into that flow, the bottom is the newest and . . . (I've already said that. Oops.)

Looking at you suggesting (I believe) the `array.prototype.reverse()` option.  
I don't know how/where to add it. I need a bit more of a hint.

I'm guessing in the `template` node, but......

---

<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:** [9 August 2019 21:50 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/6 "2019-08-09T21:50:25Z")

</div>

With a bit of messing around I could use `tail -n16` to get what I want.  
Given the list size is 16 lines.

But that has the downside that I can't reverse the output like with `tac`.

Still thinking.

(@dceejay)  
What was the edit?

Not too worried because looking I can't seem to see what was edited.  
But just asking.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 August 2019 21:57 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/7 "2019-08-09T21:57:22Z")

</div>

You can do `tail -n16 file | tac`

---

<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:** [9 August 2019 21:59 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/8 "2019-08-09T21:59:47Z")

</div>

Ah! Silly me! Yes, thanks.

Shall try it now.

Didn't think of that.

Bad news.

For reasons unknown, I don't get an output from that.

Running it from the CLI, it works.  
But putting that command into the `exec` node and pressing the button. Nothing.

I think that is because (after a bit of digging) the CLI doesn't _finish_ the command and exit.

It is locked in the command.

So alas that can't work as it is.  
☹

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 August 2019 22:27 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/9 "2019-08-09T22:27:32Z")

</div>

Works fine, are you using an absolute path ?

 ![08](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/596923a09a1e8c3f11b961e93199a5547e63f923.jpeg)

---

<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:** [9 August 2019 22:30 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/10 "2019-08-09T22:30:17Z")

</div>

Yes.

I shall try again shortly. (I'm looking at Bart's new node just now) `vcgen`.

Looks interesting/handy.

When I wasn't getting an output I tried it from the CLI and it seemed to work. I got/saw a list.  
But then when I went back to the CLI, I saw it hadn't exited.

Weird.

Standby for updates.

Here's the screens:

 ![Screenshot%20from%202019-08-10%2008-30-46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3afb32eb844356c1f9bd796cc17ad423536b87f4.png) ![Screenshot%20from%202019-08-10%2008-32-16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3d5cbea0e4c03a5c39ab999f3c481e76101a3e43.png)

I'm wanting the most recent at the top.

Yeah, ok, so why use `tac` rather than `cat`?

Here is the file I am wanting to list:  
[commands\_received.txt](https://discourse.nodered.org/uploads/short-url/3dvu97sSsc4MtJnJ3pZGYca9I7L.txt) (1.0 KB)

What I want to see is that file on the screen with the top being the most recent command.

Oh, this is what I see from the CLI if I do the command - which is just out of interest that it isn't _exiting_ - as I explained.

 ![Screenshot%20from%202019-08-10%2008-43-08](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d2dcb816c5004d6e001f184416b1223edbe89124.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 August 2019 06:23 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/11 "2019-08-10T06:23:51Z")

</div>

Look again at the command@bakman2 suggested and look at what you have used. When something doesn't work it is always worth checking for simple errors first.

---

<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:** [10 August 2019 06:33 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/12 "2019-08-10T06:33:25Z")

</div>

That being I did:

`tail -n16 | tac (filename)` rather than `tail -n16 (filename) | tac` yeah?

Silly me.

Thanks.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 August 2019 07:58 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/13 "2019-08-10T07:58:14Z")

</div>

Worse than that, you didn't have the pipe (|) at all.

You might like to work through this excellent tutorial in the "Learn Enough to be Dangerous" series (which are excellent and free to use online). With some basic understanding of how the command line works such mistakes become much less likely.  
[Learn Enough Command Line to be Dangerous](https://www.learnenough.com/command-line-tutorial/basics)

---

<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:** [10 August 2019 08:01 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/14 "2019-08-10T08:01:14Z")

</div>

I am actually going through it.

I don't know how the `|` became missing.

Could have been a typo'.

Anyway, it seems to be working now.  
Thanks.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [10 August 2019 15:44 UTC](https://discourse.nodered.org/t/lists-need-a-bit-of-help/14157/15 "2019-08-10T15:44:23Z")

</div>

`tac` is a reverse `cat`

a command which produces output (stdout) and can be 'piped' `|` to another command.  
Linux/unix is extremely powerful with the pipe command.

So in this case the `tail` sends 16 lines to stdout and through the pipe to `tac` which reverses it.
