Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 dont 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
1  
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
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.