Sometimes you may want to display the contents of a file in the columnar format while working on the command line in Linux. Luckily, there is a Linux command Column that allows you to display contents of the file in a columnar format. It is very simple and easy to use command line utility.
This command line utility converts the input file into multiple columns and you can convert the content into the columns based on any delimiter. Column command fills rows before column.
Today we are going to look at the basics of Column command and how we can use it in a most useful way to format the content. For this article, I am using Ubuntu 18.04 LTS for describing the procedure.
Syntax of Column command
The general Syntax of the column command is:
$ column [parameters] [filename]
In the [parameters], you can specify the following options:
-s specifies a delimiter character.
-c output is formatted based on total characters allowed in one row.
-t create a table, by default spaces or the character specified with –s option are used as a delimiter
-x by default, rows are filled before column, but using this parameter you can fill column before filling rows
-n by default multiple delimiters are merged as a single delimiter. You can disable this behavior using –n parameter.
-e by default, empty lines are ignored. You can disable this behavior using –e parameter.
Column command usage
For instance, I have created a test file name “test” that has content shown in the below image. Note that you can view the content of a file without opening it using cat command in Terminal.Advertisement
Launch the Terminal by pressing Ctrl+Alt+T and type:
$ cat [filename]
My test file looks like:
To see what the column command actually does, type column followed by the filename:
$ column [filename]
By entering the above command, you will see that this command has converted the content of the file into columns.
This was the simplest explanation of the Column command. Now we will see some more uses of this command.
Using a custom delimiter or a separator
You can use a custom separator in a Column command that will tell it when it should split the content into new columns.
I am using a test file that contains the content as shown in the image below. It contains the data separated by commas.
Use –t parameter to display the content in tabular format. By default, it uses space as a delimiter.
To separate the content based on a specific delimiter, use –s parameter followed by the particular separator or delimiter as shown in the below command syntax:
$ column -t [-s separator] [filename]
Add multiple delimiters using -n parameter
By default, column command merges the multiple adjacent separators in a single seperator. For instance, we have a sample file that contains the content having multiple adjacent separators as shown in the image below.
Column command will consider the multiple separators as a single one and will display the output as below
Adding the –n option will disable this behavior. Run the command in the below syntax:
$ column [-n] [-t] [-s separator] [filename]
You will notice the space between the first two columns, which implies that it has not merged the adjacent separators into a single one.
Add empty lines using -e parameter
By default, the column command ignores the empty lines. If you want to include those empty lines in your columns, use –e parameter.
For instance, I have the following sample file with a few empty lines in it.
I have used the column command but you can see that column command has not included the empty lines.
To allow the column command to include that empty lines in the output, use the command in the below syntax:
$ column [-e] [-t] [-s separator] [filename]
You will notice that the empty lines are now included in the output of column command.
Fill columns before rows using -x parameter
The Column command converts content into the columns by initially filling the rows then columns that may be inappropriate for some files. However, you can change this behavior and can fill columns before filling rows. For a clear understanding, see the below example.
In this example, I have a test file named “xyz” containing some content. My test file looks like:
Using the Column command will display the output like below. however that is not appropriate for the above type of file.
For this, we will use the –x parameter. Type column followed by –x and the filename.
$ column [-x] [filename]
Now you will notice that this output is in an appropriate format for our above sample file.
Changing the display width using -c parameter
You can stretch out the display width of the command output. Specify the width of the row by using –c parameter in the column command. In this example, I have a file named “xyz” containing some content as shown below.
Using just the column command displays the output like this:
To change the width of the display, type command followed by –c parameter. To reduce the width of the display, I have entered the command in below syntax:
$ column [-c characters] [filename]
To increase the width of the display I have entered the command as shown in the image below:
Now you can see that the width of the display has increased.
Save the columnated output
To save the formatted output that you have accomplished using Column command, you can use the below command in Terminal. The syntax of the command will be:
$ column [parameters] [filename1] > filename2
That is all for now. You have seen that through basic yet helpful examples of column command described in this article, you can get a complete grip on the command and use it to format your content which otherwise you have to do manually by hand.