Display File Contents In Column Format Within Linux

The Linux 'column' command works with delimited text files

Display a delimited file in the Linux terminal so that each delimited item is displayed within its own column. For example, here is an example English Premier League Football table that uses pipes as delimiters.

4|man city|30|51
5|west ham|30|50
6|man utd|30|50
8|stoke city|31|46

This list includes the top 10 teams, their names, the number of games they have played, and the points scored.

Several Linux commands display data in the command line. For example, the cat command displays the file exactly as it appears in the file. The tail command shows a portion of the file or all of it, as can the head command. However, none of these commands display the output in a way that makes it look good.

Ideally, you want to be able to see the data without the pipe symbol and spaced apart. That is where the column command comes in.

Basic Use of the Column Command

You can run the column command without any parameters as follows:

column <file>

This works best with files of words with spaces between the words. It doesn't work as well with tabular data as in this league table example.

The output is as follows:

pos|team|pld|pts    2|tottenham|31|61   4|man city|30|51    6|man utd|30|50    8|stoke city|31|46   10|Chelsea|30|41 1|leicester|31|66   3|arsenal|30|55    5|west ham|30|50    7|southampton|31|47  9|liverpool|29|44

Linux column basic output

Specifying the Column Width

If you know the widths of the columns, you can use the following command to separate the column by width:

column -c<number> <file>

For example, if you know the width of each column is 20 characters you can use the following command:

column -c20 leaguetable

In the case of the league table, this doesn't work well unless all of the columns a certain width. To prove this, change the league table file as follows:

pos        team         pld       pts
1            leicester    31        66
2            tottenham 31        61
3            arsenal      30        55
4            man city    30        51
5            west ham  30        50
6            man utd     30       50
7            so'ton        31        47
8            stoke         31        46
9            liverpool    29        44
10          chelsea     30        41

Now by using the following command, you can get a decent output:

column -c10 leaguetable

The problem with this is that the data in the file already looked good so the tail, head, nano or cat commands could all show the same information in an acceptable way.

Linux column command width

Specifying Separators Using the Column Command

The best way to use the column command on comma, pipe, or other delimited files is as follows:

column -s"|" -t <file>

The -s switch lets you determine the delimiter to use. For example, if your file is comma separated, you can put "," after the -s. The -t switch displays the data in a tabular format.

linux column output

Output Separators

So far this example has shown how to work with the delimiter of an input file, but what about the data when it is displayed on the screen?

The Linux default is two spaces, but maybe you want to use two colons instead. The following command shows you how to specify an output separator:

column -s"|" -t -o"::" <file>

When used with the league table file, the command produces the following output:

pos::team             ::pld ::pts
1  ::leicester        ::31   ::66
2  ::tottenham     ::31   ::61
3  ::arsenal          ::30   ::55
4  ::man city        ::30   ::51
5  ::west ham       ::30  ::50
6  ::man utd         ::30  ::50
7  ::southampton ::31  ::47
8  ::stoke city       ::31  ::46
9  ::liverpool         ::29  ::44
10 ::Chelsea          ::30  ::41

Fill Rows Before Columns

There is another switch that isn't particularly useful but is included here for completeness. The -x switch when used with the -c switch fills the rows before columns.

So what does that mean? Look at the following example:

column -c100 leaguetable

The output of this would be as follows:

As you can see, it goes down first and then across.

Now look at this example:

column -c100 -x leaguetable

This time the output is as follows:

pos|team|pld|pts    1|leicester|31|66   2|tottenham|31|61   3|arsenal|30|55 4|man city|30|51    5|west ham|30|50    6|man utd|30|50    7|southampton|31|47 8|stoke city|31|46   9|liverpool|29|44   10|Chelsea|30|41

The data goes across the screen and then down.

Linux column rows before columns

Other Switches

The only other switches available are as follows:

column -V <file>

This displays the version of the column installed on your computer.

column --help <file>

This displays the manual page to the terminal window.

Was this page helpful?