6

In another question I asked on how to hide all .pyc files when using ls, Ignacio suggested the following: ls | grep -v '.pyc$' | column

This, as I mentioned above, doesn't work precisely, since the output is occasionally misaligned:

ceasarbautista@hse140:~/Desktop/Statistics/statistics/markov$ ls
README          __init__.pyc        markov.py       matrix2graph.pyc    pathfinder.pyc      priority_dict.pyc   spanning.py
__init__.py     graph.py        matrix2graph.py     pathfinder.py       priority_dict.py    space.py        vector.py
ceasarbautista@hse140:~/Desktop/Statistics/statistics/markov$ ls | grep -v '\.pyc$' | column
README      graph.py        matrix2graph.py priority_dict.py    spanning.py
__init__.py markov.py   pathfinder.py   space.py        vector.py

While I got a satisfactory answer, I'm curious: why does column do this (and can it be fixed to print correctly)?

5
  • What does shoving it through od -c say? Oct 24 '11 at 2:48
  • gist.github.com/1308298 Oct 24 '11 at 3:25
  • 1
    In the output shown above (and in your other post), even the ls output is misaligned. Perhaps it's something with your terminal. Trying adding | cat -A to the end of the pipeline, which will expose any hidden control or escape sequences that might be corrupting the output. Oct 24 '11 at 4:52
  • So I don't have a -A (I'm using OS Lion if that matters), but presume -evt ought to do the same thing: gist.github.com/1308445 Oct 24 '11 at 5:59
  • have you tried something like this? ls -1 |column -c `tput cols` |column -t basically I used column 2 times, looks non-sense but actually works for my case here at least Dec 2 '15 at 15:57
10

When called without any options, column makes each delimited strings align to the nearest TAB-STOP COLUMN. In a terminal, that is typically on every 8th character column.. Have a look at this example:

Create a file (ztxt) containing some tab-character \t delimited strings, spread over 3 lines which end with newline-character \n:

aaa1\taaaaaaaaaaaaaa2\taaaaaaaaaaaaaaaaaaa3\taaa4
bbbbbbb1\tbbb2\tbbb3
ccc1\tccc2

Output of: column ztxt - perfectly aligned to the nearest TAB-STOP

aaa1    aaaaaaaaaaaaaa2 aaaaaaaaaaaaaaaaaaa3    aaa4
bbbbbb1 bbb2    bbb3
ccc1    ccc2
|       |       |       |       |       |       |
|-------|-------|-------|-------|-------|-------|

To align the LSH of each successive field to the one above it, you need to use the -t option, eg. column -t ztxt

aaa1     aaaaaaaaaaaaaa2  aaaaaaaaaaaaaaaaaaa3  aaa4
bbbbbb1  bbb2             bbb3
ccc1     ccc2

If all your data is in one long stream without any newline breaks, you can uss a filter introduce them; eg one every 4 fields. sed can do it with this command.

sed -re 's/(([^\t]*\t){3}[^\t]*)\t/\1\n/g' 

By default, the column command will merge multiple adjacent delimiters into a single delimiter.. To cater for this in the sed filter, it also needs:

sed -re 's/\t+/\t/g;' 

So the command to split up a continuous stream of tab-delimited strings, as every 4th string is:

<ztxt sed -re 's/\t+/\t/g;s/(([^\t]*\t){3}[^\t]*)\t/\1\n/g' | column -t  

The output of such a contionuous input stream is (using the original sample input, but modified by replacing the original newlines with tab-characters--it must still keep its trailing \n):

aaa1      aaaaaaaaaaaaaa2  aaaaaaaaaaaaaaaaaaa3  aaa4
bbbbbbb1  bbb2             bbb3                  ccc1
ccc2
3
  • any idea why this ls * -1 |column -t fails horribly? in my case, using the column or not, give the same (one column) output. I've been trying to use column for a long long time and it never worked properly :(. I wonder if there is some column alternative that we can fix the column width even with loss of displayed data to finally have an actual pretty looking and easy to read output. My last attempt will be to code some script to do all that :/ Dec 2 '15 at 15:34
  • and this isn't the accepted answer because..? great detailed post. Jan 28 '18 at 20:09
  • @underscore_d Detailed but have you tried this? -t option prints out one single column so this doesn't work unless the file is already in columns to begin with.
    – mchid
    Mar 4 '18 at 4:48
2

For the multiple delimiters bit. Annoyingly the -n option is only available in Debian, which handled multiple delimiters.

column -t -n <file>
0

If ls output is garbled, it indicates that there are non-printable characters within one or more filenames. I therefore recommend always to use the option -q with ls.

Any non-printable characters will then be printed out as a question mark.

So please try:

 ls -Cq
1
  • 1
    The ls output wasn't garbled; it was that by default column was aligning to tab stops, not within columns. Jan 28 '18 at 20:14
0

The problem with column is that it splits on \s+, I don't see how to make it split on a single tab. I wrote this much more flexible script to solve this problem, that anybody is free to use:

http://itmat.greg.s3.amazonaws.com/display.pl.gz

Here's the full usage:

  --------------------------------------------------------------------------
 | This script displays tab delimited text in properly aligned columns.     |
 |                                                                          |
 | Usage: perl display.pl  [num lines] [options]                      |
 |                                                                          |
 | Where  is tab delimited.                                           |
 |                                                                          |
 | If  is followed by a positive integer N, only the first N lines    |
 | will be returned.                                                        |
 |                                                                          |
 | If lines are long, this script works bests if piped to "less -S".        |
 |                                                                          |
 | [options]                                                                |
 |                                                                          |
 | -noheader :  A header line is assumed by default.                        |
 |                                                                          |
 | -nodots   :  By default if a column is wide and an entry in that column  |
 |              is short, dots are put after it to help line things up.     |
 |              Use this option to just print spaces, no dots.              |
 |                                                                          |
 | -dotcols  :  Use this to specify specific columns to have dots     |
 |                    (see the -nodots option).   must be a comma     |
 |                    separated list of positive integers w/o whitespace.   |
 |                                                                          |
 | -cols  :  Use this to specify a subset of columns to output.       |
 |                  is a comma separated list of positive integers    |
 |                 and/or spans of positive integers.  Examples of valid    |
 |                 lists are: 4,6,12 or 4-10 or 1-4,12,15,4-7.  Columns     |
 |                 can be repeated and don't have to be in numerical order. |
 |                                                                          |
 | NOTE: This script was hacked from a much more complicated script, so     |
 | the code is full of all kinds of irrelevant stuff.                       |
  --------------------------------------------------------------------------
2
  • "I don't see how to make it split on a single tab." What, does -s'[tab]' not work for you? Where, as usual, [tab] means either type a literal tab in your script or press Ctrl+V, Tab at the shell prompt. Jan 28 '18 at 20:12
  • You can post the script here so that people can copy and paste the script. We have no idea what is in the compressed file so this is shady. Most of the time people post scripts in answers.
    – mchid
    Mar 4 '18 at 4:54
0

To show non-printable characters (as C escape codes whenever possible) in your ls output you also can use the -b option to the ls command:

ls -Cb | grep -v '\.pyc$' | column -t
1
  • 1
    Non-printable characters are not relevant here; the issue is the alignment, and it's off for the reasons the top-voted answer explained, not because of control or oddly spaced characters in filenames. Jan 28 '18 at 20:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.