3

I'd like to print long console lines as pages which are below each other.

For example

AAAAAAABBBBBBBXX
CCCCC
DDDDDDDEE
FFFFFFFGGGGG
IIII

should be displayed as

AAAAAAA
CCCCC
DDDDDDD
---
BBBBBBB

EE
---
XX


===
FFFFFFF
IIII

---
GGGGG


===

instead of wrapping lines. Basically for a fixed number of lines ("chunk") and if there are line widths beyond a certain column, a new "page" should be created and displayed on the console. It should be done on chunks of lines, to avoid that for many lines the "pages" appear at the very bottom. Also there may be multiple "extension pages" needed, if the line are very long ("XX" in this example). Note that at the end, empty lines may be added in the input to have a complete chunk of data. "---" are page separators and "===" a chunk separators.

The basic idea is that display something like tabular data aligns. Is there an easy way to do that?

Gere
  • 241
  • And you also want blank lines to be printed in the second "page" for lines that had less than the threshold characters? – terdon Mar 15 '16 at 14:05
  • Yes, I suppose that makes most sense. – Gere Mar 15 '16 at 14:11
  • And what about very long lines? Say your threshold is 5 and you have a line with 100 characters? Should that create 20 additional pages or just one with the first 5 and one with the other 95? – terdon Mar 15 '16 at 14:13
  • It should be many pages, as there is usually a hard limit on the terminal width. And actually this whole operation should be done on chunks of lines (e.g. first 10 lines -> pages; next 10 lines -> pages; ...) so that the information doesn't spread too much. – Gere Mar 15 '16 at 14:36
  • OK, then please [edit] your question and clarify all these details. – terdon Mar 15 '16 at 14:54
  • I've edited the example to cover the corner cases, that you pointed out. – Gere Mar 15 '16 at 15:09

1 Answers1

1

This isn't quite what you asked for, but here's what I do:

Pipe the output into less with the -S option (chop long lines):

my_command_that_outputs_tables | less -S

Then, inside the less viewer, the left/right cursor keys will "scroll" left and right through your data, more-or-less a page at a time. Hit q to exit, or ? for help.


And, here's how to do what you want:

my_command_that_outputs_tables | expand | tee >(cut -c1-79) >(sleep 1; cut -c80-159) | {sleep 2; cut -c160-}

This example is for data that is up to three 80-column screens wide, with 8-space tab stops.

  • expand converts tabs to spaces. Without this data with tabs in it would not fit the terminal.
  • tee duplicates the data into three identical streams, in this case; two as "files", one as stdout. You can add as many "file" outputs as you like.
  • >(...) is bash syntax to treat an output file as if it were a pipe.
  • sleep 1 and sleep 2 are delays that make the pages come out in the right order. This is a bit of a hack; there may be a better way.
  • cut -cM-N takes a vertical slice out of the input. M is the first column, counted from 1, and N is the last column. If N is omitted then it takes all the characters to the end of the line.

So, it takes your data, duplicates it into three streams, and then carves up each stream vertically.

ams
  • 5,807
  • 1
  • 20
  • 27
  • Not quite what I asked, but definitely something very useful! I might even consider this. The only reason left for my initial request would be to see all at one glance without scrolling. – Gere Mar 17 '16 at 16:56
  • @Gerenuk I came up with a proper solution, although I still like less better. – ams Mar 17 '16 at 21:18
  • Cool. It's definitely an interesting approach! – Gere Mar 18 '16 at 16:47