column Command in Linux



column command in Linux shows the file’s data in columns. This command takes the input from a file or standard input and breaks the given input into columns. It fills rows before columns and ignores the empty spaces from the input. All in all, this command formats the given input into a table-like structure.

In this tutorial, we’ll show you how to use the column command in Linux to format an output in the form of rows and columns.

column Command in Linux

column is a command line utility in Linux that scans input from a file or standard input and formats it into columns. It splits the input into columns, fills data row-wise, ignores unnecessary spaces, and presents the output in a tabular format.

Basic Syntax of column Command

To use the column command in Linux, you need to follow the below-mentioned syntax −

column [options] [file...]

Here, "options" represent the optional flags that are used to customize the behavior of the column command. The "file" means a single or multiple file whose content needs to be formatted. If we don’t specify any file, the column command takes input from standard input.

Common Options with column Command

The column command in Linux accepts various options to format the output into columns. These options let you customize the input data and display the output in a tabular format. The table given below illustrates some commonly used options with the column command and their descriptions −

Option Description
-c width, --output-width width It specifies the number of characters to set the maximum column width.
-d, --table-noheadings It prints the output table without a header.
-s, --separator It specifies a single-character column separator.
-t, --table It creates a table by automatically determining the number of columns based on the input data and aligning the content accordingly.
-x By default, the column command fills rows before columns. However, if the "-x" option is specified, the column command fills columns before rows.
-V, --version It shows the installed version of the column command.
-l, --table-columns-limit It specifies the maximum number of input columns
-R, --table-right It right-aligns the data in the selected columns.
-h, --help It shows all the necessary details/help regarding the column command.
-J It shows the output in JSON format.

Accessing column Command Manual Page

The previous section highlights only a few options that are most commonly used with the column command. However, there are a lot more options supported by the column command. You can access the column command’s manual page to learn more about this command and its options −

man column
Accessing column Command Manual Page

Alternatively, you can access the column command’s help page to get the necessary details about this command −

column --help

The help page shows the command’s basic syntax, valid options, and their to the point description −

Accessing column Command Manual Page 2

Checking the Installed Version of column Command

The column command is part of the util-linux package. By executing column -V, you can get the installed version of the column command −

column -V
Checking Installed Version of column Command

Installing column Command

The column command is usually pre-installed on many Linux distributions as part of the util-linux package. However, if it is not installed (for some reason) in your system, you will need to install the util-linux package to use the column command. Different Linux distributions use different package managers to install the util-linux package, as shown in the following snippet −

#for installing the column command on Debian-based systems
sudo apt install util-linux

#for installing column on Fedora
sudo dnf install util-linux

#for installing column on CentOS or RHEL
sudo yum install util-linux

#for installing column on Arch Linux
sudo pacman -S util-linux

You can verify the installation of the column command by checking its version or by accessing its manual page.

Examples of column Command in Linux

Let’s understand how the column command works in Linux using some practical examples −

Displaying the Content of a Text File in Columns

We have already created a text file that contains the following content −

cat exampleFile.txt
Displaying Content of Text File in Columns 1

Let’s use the column command to show the file’s data in the form of columns −

column exampleFile.txt
Displaying Content of Text File in Columns 2

Similarly, you can use the -x option with the column command to convert the rows into columns. For instance, we have a file named "example1.txt" whose content is as follows −

cat example1.txt
Displaying Content of Text File in Columns 3

Let’s convert the rows into columns by executing the column command −

column -x example1.txt

The file’s rows are successfully converted into columns using the column command −

Displaying Content of Text File in Columns 4

Displaying File Content in Tabular Format Using a Specific Delimiter

The column command uses a whitespace as a column separator, by default. However, we can also separate columns using a custom delimiter. For this purpose, we need to use the -s option with the column command, as shown in the following example.

First, let’s check the file’s content by running the cat command −

cat file1.txt
Displaying File Content in Tabular Format 1

Now, let’s split the file’s content into columns and use the "," as a delimiter −

column -t -s "," file1.txt

Here, the -t option tells the column command to create a table from the given file’s content, and -s "," ensures that the table’s columns are separated by commas −

Displaying File Content in Tabular Format 2

Separating File’s Output

The previous example shows that the column command separates the output columns with two whitespaces. However, we can separate it with a separator of our choice. To do this, we must use the "-o" flag with the column command as follows −

column -t -s "," -o "||" sampleFile.txt

This time, the output columns are separated using the specified pipeline symbol "||" −

Separating File’s Output

How does Column Command Treat Empty Lines?

By default, the column command in Linux ignores empty lines in the input file when formatting the data into columns. The following command retrieves the content of file2.txt −

cat file2.txt
Column Command Treat Empty Lines 1

The output shows that there are some empty lines between the file's content. Let's use the column command to see how it formats the file's content −

column -t -s "," file2.txt

The column command skips the empty lines to ensure that the output is neat and without unnecessary blank rows −

Column Command Treat Empty Lines 2

That’s all about the usage of the column command in Linux.

Conclusion

The column command in Linux is a handy tool for formatting data into a table-like structure. It scans input from a file or standard input, splits it into columns, and aligns the data in a table format while ignoring empty lines.

In this tutorial, we explained several aspects of the column command, including its basic syntax, available options, installation, and practical examples. Using the column command with different options like -t and -s helps users to effectively organize and display data in a structured and readable format.

Advertisements