Display File Contents In Column Format Within Linux

Display Tabular Data With Column Command
Display Tabular Data With Column Command.


This is a fairly straight forward guide showing how to display a delimited file in the Linux terminal so that each delimited item is displayed within its own column.

What do I mean by this? Let us look at the English Premier League Football table.

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

As you can see I have listed the top 10 teams, their names, the number of games they have played and the points scored.

There are a number of commands I can use to display that data in the command line.

For example the cat command will display the file exactly as it appears in the file. The tail command can be used to show a portion of the file or all of it, as can the head command.

None of these commands however 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 <filename>

This works best with files of words with spaces between the words. it doesn't work as well with tabular data as in our 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

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<width> <filename>

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

column -c20 <filename>

Again in the case of the league table this isn't very good unless I made all of the columns a certain width.

To prove this I changed the leaguetable 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 I get a decent output:

column -c10 leaguetable

The problem with this of course is that the data in the file already looks good so I could use tail, head, nano or cat to show this same information in an acceptable way.

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 <filename>

The -s switch lets you determine the delimiter to use. For example if your file is comma separator you can put "," after the -s.

The -t switch displays the data in a tabular format.

Output Separators

Thus far I have shown how to determine the delimiter of an input file but what about the data when it is displayed to the screen.

The default is two spaces but maybe you want to use 2 colons.

The following command shows you how to specify an output separator:

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

When used against the league table file it 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 which I haven't really found overly useful but is 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:

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

As you can see it goes down the way 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

As you can hopefully see the data goes across the screen and then down.

Other Switches

The only other switches available are as follows:

column -V

This displays the version of column installed on your computer.

column --help

This displays the manual page to the terminal window.


More From Us