bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: cols command for columnar output


From: Stephen Jungels
Subject: RE: cols command for columnar output
Date: Mon, 8 Aug 2005 12:28:53 -0400

Perhaps some examples will clarify the difference.  Assume the file count
contains the numbers from 1 to 99, one per line.  Here is the cols command
formatting that file:

$ cols -w 60 count
1  8   15  22  29  36  43  50  57  64  71  78  85  92  99
2  9   16  23  30  37  44  51  58  65  72  79  86  93
3  10  17  24  31  38  45  52  59  66  73  80  87  94
4  11  18  25  32  39  46  53  60  67  74  81  88  95
5  12  19  26  33  40  47  54  61  68  75  82  89  96
6  13  20  27  34  41  48  55  62  69  76  83  90  97
7  14  21  28  35  42  49  56  63  70  77  84  91  98

Here is the pr command you suggested.  Note that it doesn't actually provide
multi-column formatting:

$ pr -t -w 60 count
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...

Here is a pr command that provides multi-column formatting.  Note that I had
to provide -14 to request 14 columns of output:

$ pr -t -w 60 -14 count
1   9   16  23  30  37  44  51  58  65  72  79  86  93
2   10  17  24  31  38  45  52  59  66  73  80  87  94
3   11  18  25  32  39  46  53  60  67  74  81  88  95
4   12  19  26  33  40  47  54  61  68  75  82  89  96
5   13  20  27  34  41  48  55  62  69  76  83  90  97
6   14  21  28  35  42  49  56  63  70  77  84  91  98
7   15  22  29  36  43  50  57  64  71  78  85  92  99


The last command works, but 14 is a "magic number" that I had to guess.  The
cols command calculates this for you.

More complex input would reveal additional problems with the pr command.
For example:

  -- all columns are the same width, so the data isn't packed very
efficiently
  -- strings that are significantly longer than other strings in a column
are truncated.  This is frequently unsatisfactory.

The pr command wasn't really designed for multicolumn output to a terminal
screen and people shouldn't have to live with its shortcomings.  I see
people writing slow scripts in bash and awk all the time that solve only
half the problem of multicolumn output.  Since they are trying to emulate
the output of the ls command, why not go right to the source and provide a
speedy c routine that has all of the ls command's multicolumn features?

The idea of fixing the pr command is interesting, but I assume there are a
lot of scripts out there that depend on its current behavior, so this might
be more disruptive than introducing a new command.

--Stephen



-----Original Message-----
From: address@hidden
[mailto:address@hidden Behalf Of
address@hidden
Sent: Monday, August 08, 2005 7:16 AM
To: Stephen Jungels
Cc: address@hidden
Subject: Re: cols command for columnar output


Stephen Jungels wrote:
> Because I needed nice columnar output similar to the ls command's simple
> file list, I went in and ripped out the columnar logic from GNU ls and
> created a nice simple "cols" command.  It does one thing only and does it
> well, like a good UNIX command should.
>
> (pr isn't good enough because it requires you to guess how many columns
will
> fit across the screen, and it truncates long strings.  cols makes each
> column just wide enough for its longest string and fits as many columns as
> possible across the screen.)

Why not fix pr if it doesn't do as required?
Anyway...

pr -T -W$COLUMNS

Add -J to control truncation/wrapping

--
Pádraig Brady - http://www.pixelbeat.org
--


_______________________________________________
Bug-coreutils mailing list
address@hidden
http://lists.gnu.org/mailman/listinfo/bug-coreutils





reply via email to

[Prev in Thread] Current Thread [Next in Thread]