On this page

Sometimes, while working on the command line in Linux, you might want to display the contents of a file in columnar format. You'll be glad to know there's a command line utility in Linux that lets you do this. The tool's name is column, and we'll discuss the basics of this command using some easy to understand examples.

But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux column command

The column command in Linux lets you columnated lists. Following is its syntax:

column [-entx] [-c columns] [-s sep] [file ...]

And here's how the tool's man page defines it:

The column utility formats its input into multiple columns. Rows are filled before columns. Input is taken from file operands, or, by default, from the standard input.

Following are some Q&A styled examples that should give you a better idea on how the column command works.

Q1. How to use column command?

Basic usage is pretty easy. Suppose you a have file named 'test.txt' that contains the following information:

12345678910

Now, to columnate the content of this file, use the column command in the following way:

FYI, here's how the column command man page explains the -t and -s command line options:

-s Specify a set of characters to be used to delimit columns for the -t option.

-t Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with the characters supplied using the -s option. Useful for pretty-printing displays.

Q3. What about cases with multiple delimiters?

In the previous example, you saw the original content contained pipe '|' as a delimiter. So the column command used this delimiter to produce columnar format output. But what if there were two pipes in some entries. For example, see the first line here:

By default, the column command merges multiple adjacent delimiters into a single delimiter. However, if you want, you can use the -n command line option to disable that behavior. So in that case, the column command would become:

column -n test.txt -t -s "|"

Q4. How column command deals with empty lines?

By default, empty lines are ignored by the column command. However, if you want, you can suppress this behavior by using the -e command line option.

For example, file content line this:

No.|Country|Yes/No01|India|Y02|US|Y03|Australia|Y

04|China|N05|Russia|Y06|Japan|Y

07|Singapore|Y08|South Korea|N09|Finland|Y10|Ireland|Y

Will come out like:

No. Country Yes/No01 India Y02 US Y03 Australia Y

04 China N05 Russia Y06 Japan Y

07 Singapore Y08 South Korea N09 Finland Y10 Ireland Y

using the following command:

column -e test.txt -t -s "|"

Conclusion

Depending on the kind of work you do on the Linux command line, the column command could be of great help to you. In this tutorial, we have discussed majority options offered by the tool. For more info, head to its man page.

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.