Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to take some single column data and display it in a tty as columns, much like ls does. Basically, you can think of this as trying to take the output from ls -1 and make it look like the normal output of ls, where the number of columns and the space given to each column is optimized.

The column command can't easily be coerced into taking up a certain width, and the pr utility doesn't dynamically change the number of columns.

With all the text parsing commands available, I just can't believe there isn't some way to make this happen out of the box.

This would be a fairly complex little shell script, because in order to calculate the optimal column width you need to know how many items will be in your column (to calculate the length of the longest item), which you don't know until you figure out how many columns there will be, which depends on the width of each column. Because of the circular reasoning, it seems like iterative refinement is the only way to go. Not hard, but not a one-liner either. A built-in would be a much better solution if it exists.

share|improve this question
Awk is commonly used for this. – technosaurus Mar 20 '12 at 3:18
As you said the column width can't be known before all lines are needs to be examined. Most simple filters work as a pipe (except sort which uses tmp file). This makes it no so trivial and depending your case it could be easier for you to just writ this script based on what you exactly need. – Cougar Mar 22 '12 at 8:52

You'll want the pr command:

$ seq 25 | pr -t -5
1         6         11        16        21
2         7         12        17        22
3         8         13        18        23
4         9         14        19        24
5         10        15        20        25
share|improve this answer

You may be happy with

My Command | column -t 

See Easy way to columnize STDOUT (format text in columns)?

If you want to arrange several line into one, you may want to use sed command as such

ls -1 \
| sed -e 'N;N;N;s/\n/\t/g' \
| column -t 

In this case, you are making one line from four (three N; is four minus one ) then changing new line \n into tab (\t) for each new lines (\t) in buffer (g).

The last line column -t make it into optimal columns.

The ending \simply escape new line characters. You may want to type

ls -1 | sed -e 'N;N;N;s/\n/\t/g' | column -t 


Hope you are enjoying.

share|improve this answer
I'm trying to deal with single column data that I want to put into multiple columns. column is only useful for arranging multi-column data. ie: ls -1 | column -t doesn't re-arrange things to look like ls – Doug J May 28 '14 at 16:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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