Martijn Dekker wrote:
>> Janis Papanagnou wrote:
>>
>>> Or use of the (almost forgotten?) pr(1) command (supported with a bunch
>>> of options to suppress headers etc. etc.).
>
> This may be an even better idea. The -t option completely turns off page
> separation and padding with empty lines, so 'pr -t' equals 'cat'. So 'pr
> -t -n' is a straightforward line numbering filter for arbitrary input
> text. It's output is identical to that of the non-standard 'cat -n'.
>
> I've also verified this is POSIX and works on macOS, the BSDs, Linux,
> Solaris, and (as verified at <
https://unix50.org/>) all the way back to
> AT&T UNIX System III (1982), so this should be extremely portable.
>
>
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pr.html
>
> As an aside, something like 'pr -t -3' will format text into 3 columns.
> I've been wanting a portable way to do that for a while (as the 'column'
> command is not portable).
>
> Thanks for reminding us of this useful utility!
This really excited me for a second--I have a blog post [1] that offers a
sequence of commands to extract some data from Git and display it in a nice
table, and I was always slightly annoyed that I had to offer an alternate
version for users who didn't have column(1) installed. (The pipeline
produces data in CSV format but with semicolons for delimiters. If you have
column(1) you can format it nicely with "column -s ';' -t"; my fallback was
"sed -e 's/;/\t/g'", which gets most of the way there but produces jagged-
looking columns sometimes.) It would be great to be able to replace those
two commands with a single one that would work on macOS, Linux, BSD, etc.
It took me some tweaking to get pr(1) to produce output similar to what I
wanted. Starting from an input like
3 years, 1 month ago;Parker Moore;v1-stable
12 months ago;Ben Balter;pages-as-documents
10 months ago;Jordon Bedwell;make-jekyll-parallel
I added
tr ';' '\n'
to the pipeline to put each data "cell" on its own line. Then I could run
pr -t -3 -a -i1000 -w`tput cols`
to produce output like
3 years, 1 month ago Parker Moore v1-stable
12 months ago Ben Balter pages-as-documents
10 months ago Jordon Bedwell make-jekyll-parallel
Not too bad! The arguments I used were as follows:
* -t: Omit headers and footers and don't print extra blank lines at the end
of the output.
* -3: Produce three-column output.
* -a: Use the first three input lines as the values in the first output row;
the next three input lines as the values in the next output row; and so
on. Without this option, the first three input lines would become the
first, second, and third *rows* in the first output *column*.
* -i1000: This is a kludge to prevent the output from including any tabs--I
wanted spaces to be used for alignment instead. (What this is really
saying is to format the output as if the tab stops were at columns 1001,
2001, 3001, etc., effectively ensuring that no tabs are actually printed.)
* -w`tput cols`: Specify that the output should be exactly as wide as the
current terminal. (I used the backtick syntax because I wanted something
that would work equally well under bash, zsh, and tcsh, the latter of
which is still used by many Mac people.) You could of course use something
like -w72 to specify that you want 72-column-wide output.
The main downsides (for my use case) are these:
* The columns are always equally sized. It's not too egregious in the
example above, but it can look really weird when one column contains very
short values and another contains very long values--there will be a ton of
empty space after the former.
* The output width has to be specified manually; as far as I can tell,
there's no option to produce output that's just as wide as it needs to be
but no wider. (Of course, this "limitation" makes perfect sense in the
tool's original context, where the paper has a certain width and you
always want the output to be sized to that width.)
(If I'm wrong about either of these, I would gladly be corrected.)
In contrast, column(1) does both of these things in the way I'd prefer: it
sizes each column according to its widest value, leaving only two spaces
between the right edge of one column and the left edge of the next. The
total width of the output is determined by the values in the input. pr(1)
will keep its columns the same size, even when this requires that the values
in one column be truncated (and even when a different column still has
plenty of whitespace around it).
I could see pr(1) being perfectly useful for some formatting tasks (and I'm
always delighted to learn about a "new" tool that has quietly been sitting
on my computer since I bought it!). In general, though, I would guess that
if you want a portable replacement for column(1) then you might be better
off writing a script in portable Awk instead.
[1]:
https://esham.io/2017/07/git-branch-authors
Best regards,
--
Benjamin Esham
https://esham.io