100 Useful Command-Line Utilities

by Oliver; 2014

54. column

Suppose you have a file with fields of variable length. Viewing it in the terminal can be messy because, if a field in one row is longer than one in another, it will upset the spacing of the columns. You can remedy this as follows:
cat myfile.txt | column -t 
This puts your file into a nice table, which is what the -t flag stands for. Here's an illustration:

image

The file tmp.txt is tab-delimited but, because the length of the fields is so different, it looks ugly.
With column -t:

image

If your file has many columns, the column command works particularly well in combination with:
less -S
which allows horizontal scrolling and prevents lines from wrapping onto the next row:
cat myfile.txt | column -t | less -S
column delimits on whitespace rather than tab by default, so if your fields themselves have spaces in them, amend it to:
cat myfile.txt | column -s'      ' -t | less -S
(where you make a tab in the terminal by typing Cntrl-v tab). This makes the terminal feel almost like an Excel spreadsheet. Observe how it changes the viewing experience for this file of fake financial data:

image

<PREV   NEXT>