Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Given perhaps comma- or tab-delimited input, I'd like to present a series of appropriately padded columns to stdout, so I can easily scan columnar information which would otherwise present rather messily.

I've tried troff-based solutions and while the simple demos have worked, feeding the command actual input has resulted in bizarre errors. I've currently resorted to using a sed-based method hack which is rather slow...

EDIT: column is quite a useful tool, however it'd be really awesome if I the columns had, say, a pipe character (|) between them so they do not appear to "float" in space and I can easily distinguish where each starts.

PS. This post's title used to read 'ASCII "table"', not 'ASCII-art table'. Edited to try and remove confusion.

share|improve this question

2 Answers 2

This will columnate the input file, adding a | character to surround each column.

 sed -e 's/^/| /' -e 's/,/,| /g' -e 's/$/,|/' inputfile | column -t -s,

Sample run (using a readily-available colon-delimited file):

$ head -4 /etc/passwd | tr : , | \
  sed -e 's/^/| /' -e 's/,/,| /g' -e 's/$/,|/' | column -t -s,

| root    | x  | 0  | 0  | root    | /root      | /bin/bash  |
| daemon  | x  | 1  | 1  | daemon  | /usr/sbin  | /bin/sh    |
| bin     | x  | 2  | 2  | bin     | /bin       | /bin/sh    |
| sys     | x  | 3  | 3  | sys     | /dev       | /bin/sh    |
share|improve this answer
    
+1 for the cool demo. –  Joseph R. Feb 9 at 23:28

Assuming a CSV file, you can use column(1) like so:

column -ts, your_file

This is included in the bsdmainutils package on my Debian distribution, so I'm not sure how portable it is.

Two more things to note:

  1. The above example is simplistic; explore the man page for more details on how to format your output.
  2. It does not fare well with quoted fields containing commas. I.e., it would consider a,b,"c,d" as four columns not three.
share|improve this answer

Your Answer

 
discard

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.