Sometimes when I am searching for a song on my machine, I use the following:

locate *artist*.mp3 | sort | less

The thing about this is that the sort will buffer until all files have been located, then sort it all in one operation, and only then will pipe all of that to less to display it.

I am curious if there is any way to sort inline, so the lines are sorted and displayed immediately as they are generated by locate. Like sort -f as in tail -f. I suppose this will need to be a single-command replacement for sort | less because it will need its own buffer for the output, I guess probably using curses or similar.

I am asking not specifically to try and get an answer to this question, but more because I am wondering if there is some kind of elegant *ix solution for something like less but for showing a buffer that is constantly being modified, not a basic fifo stream. sort is the only usecase I could think of to demonstrate this.

Maybe there is a way to do the sort into a temporary RAM buffer and have less periodically update that buffer when it changes?

Apologies for rambling, I'm finding it hard to phrase the question when I don't know what it is that I don't know.

  • "I am curious if there is any way to sort inline, so the lines are sorted immediately as they are generated by locate"... but don't you need the whole dataset first to determine the order? If you output line 1 but then realise line 2 comes first, you need to redraw the whole screen – Panki Jun 13 '19 at 6:20
  • @Panki Yes, but you could potentially re-sort the input every time you get a new line (or maybe do something smarter...) less has a | command to run an external command on part of the buffer (e.g. from the top until the current screen, which could be the last point buffered so far), maybe that gets close, but not exactly what the OP suggests... I think less could potentially be modified to do what OP suggests. I'm not aware such a modification already exists, but I think that would be a good starting point. – filbranden Jun 13 '19 at 6:47
  • @Panki yes, I want it to redraw the whole screen when new lines come in. – localhost Jun 13 '19 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.