Different Ways to Use Column Command in Linux

Have you ever been in a situation to work with CSV files and produce output in structured tabular format? Recently I was working with data cleansing on a file that is not in a proper structure. It has so many whitespaces between each column and I have to convert it to CSV format to push to the database. After cleaning and creating the output in CSV format, my output is not visually appealing to verify data integrity in the CSV file. This is the time the “Column” command comes in handy to me.

According to manpage, the column command “columnate lists”. In simple words, the column is a simple utility that can format your output into a column format (rows and fields) based on the structure of your source file. The column command is part of the util-linux package.

An important point to note here is column command behaves differently in Debian-based and Rhel-based distros. The reason is Debian-based distro uses “column” from bsdmainutils instead of util-linux. The upstream version of the column command is newer than the bsdmainutils package. Take a look at the bug report to know more about this.

$ dpkg -S $(which column)
Which Column
Which Column

For demonstration purposes, I am using CentOS 7 and will show different options between Ubuntu and CentOS 7. To check the column version run the following command. This command will also show the util-linux package version.

$ column --version  # will not work in Debian/ubuntu

You can also check the version of util-linux by running the below commands.

$ rpm -qa | grep -i util-linux   # Redhat,Centos,Fedora,Amazon Linux
$ dpkg -l | grep -i util-linux    # Ubuntu

Before using the column command a good place to start will be the man page and explore its options.

$ man column

List File Content in Tabular Format

The column command can create a table by passing the filename as an argument along with the -t flag. I am using /etc/passwd as the input file.

$ column -t /etc/passwd
List File Contents in Table Format
List File Contents in Table Format

Looking at the above image, you may think this is not what we expected and the output may look weird. Yes! You are right. Columns consider space as the default delimiter when creating a table. This behavior can be overridden by passing a custom delimiter.

Custom Delimeter

Custom delimiters give you a wide range of options to work with. To create a custom delimiter use -s flag followed by a delimiter. Now we will use “:” as a delimiter to split /etc/passwd file.

$ column -s ":"  -t /etc/passwd
Delimiter
Delimiter

Look at the above image where the table is nicely formatted and structured. From util-linux version 2.23 option -s has been changed to not be greedy.

Now run the same command in Ubuntu and the result will be greedy. This is because column command (bsdmainutils) on Ubuntu will treat multiple adjacent words as a single word.

$ column -s ":"  -t /etc/passwd
Greedy Output
Greedy Output

To overcome this behavior use -n flag.

$ column -t -s ":" -n /etc/passwd             # Only on Debian/Ubuntu
Non Greedy Output
Non-Greedy Output

Ignore White Empty Lines in File Output

When you have blank lines in your input file, the column command by default ignores it. See my input file which is in CSV format and I added a blank line between every line. Now let’s create a table as we did before with this input file.

$ column -t -s ";" dummy.txt
Ignore Empty While Lines
Ignore Empty While Lines

From the above image you can see my input file dummy.txt has empty lines and when I try to create a table, empty lines are ignored.

Note: This is the default behavior for both the “bsdmainutils/util-linux” variant of the column command. But column (bsdmainutils) has the option to override this behavior by passing -e flag.

$ column -e -t -s "," dummy.txt        # Only on Debian/Ubuntu
File White Lines
File White Lines

From the above image, you can see the table is formatted properly and the empty lines are not ignored.

File Output Separator

By default, two white spaces will be used as output separators. This behavior can be overridden by passing -o flag. You will not have an output separator option available in the column (bsdmainutils).

$ column -t -s "," -o "||" dummy.txt	# Only on Rhel based distro
File Output Seperator
File Output Separator

Convert File Rows into Columns

Using the -x the flag you can convert rows into columns. This behavior is the same in both the rhel and ubuntu variants of the column command. This is a very useful feature when you have to grab a certain field through the awk or column command then convert it to the header for your CSV file.

$ column -x fillcols.txt
Convert File Rows to Columns
Convert File Rows to Columns

When you run the column command without using any flags the behavior will be the same as passing -x flag.

Find Column Size

The column uses an environmental variable ($COLUMNS) to find out the size of your terminal and based on the size use the echo command, table size will be displayed in the terminal.

$ echo $COLUMNS

Take a look at the image below. Initially, I resized my terminal to have $COLUMNS the size set to 60 and ran the column command. Again I resized my terminal to have $COLUMNS the size set to 114 and ran the column command again. You can see the difference in how the column prints the table when we resize the terminal.

$ column -t -s ":" /etc/passwd | head 5
Resize Column Sizes
Resize Column Sizes

That’s it for this article. If you have any feedback please provide it in the comment section.

Karthick
A passionate software engineer who loves to explore new technologies. He is a public speaker and loves writing about technology, especially about Linux and open source.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

3 thoughts on “Different Ways to Use Column Command in Linux”

  1. I found your post. A very nice intro to the ‘column‘ command.

    I had encountered an issue. I have a tab-delimited file with all rows sharing the same number of columns (including the header), except that the first field in the header row is an empty string.

    I tried the column command, but never get the header row aligned properly with the remaining rows (The empty string is ignored for some reason)

    Reply
    • @Ubuntu User

      I have not tested it out on Ubuntu 20.10. But I have tested it on Ubuntu 20.04.

      tecmint@LinuxHowTo:~$ column --version
      column: invalid option -- '-'
      usage: column [-txne] [-c columns] [-s sep] [file ...]
      tecmint@LinuxHowTo:~$ column -version
      column: invalid option -- 'v'
      usage: column [-txne] [-c columns] [-s sep] [file ...]
      tecmint@LinuxHowTo:~$ lsb_release -a
      No LSB modules are available.
      Distributor ID:	Ubuntu
      Description:	Ubuntu 20.04.2 LTS
      Release:	20.04
      Codename:	focal
      
      Reply

Leave a Reply to karthick sudhakar Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.