Lists. Need a bit of help

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

Here is part of the flow and the list generation:

[{"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.

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.

Sorry.

Example file:

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 << 

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:

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.

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......

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.

You can do tail -n16 file | tac

1 Like

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.
:frowning:

Works fine, are you using an absolute path ?

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:

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 (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.

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.

1 Like

That being I did:

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

Silly me.

Thanks.

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

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.

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.