Different Ways to Use Column Command in Linux


If you're a Linux user, you're probably familiar with the command-line interface. It's a powerful tool for working with files, directories, and other aspects of your system. However, if you're working with large amounts of text data, it can be challenging to make sense of everything. That's where the column command comes in. This command allows you to format text into columns, specify delimiters, align columns, wrap text, and even sort columns of data. In this blog post, we'll explore the different ways to use the column command in Linux and how it can help you work more efficiently with text files.

What is the Column Command?

The column command is a Linux utility that helps you format text into columns. By default, it will separate columns by any whitespace characters (such as spaces or tabs), but you can also specify a delimiter of your choice. This command can be especially useful when working with text files that have a lot of data, as it can make that data much easier to read and manipulate.

Basic Usage

The basic usage of the column command is pretty straightforward. To use it, simply pipe the output of another command into column. Here’s an example −

$ ls -l | column -t total 8 drwxr-xr-x 2 user user 4096 May 6 14:45 Desktop drwxr-
xr-x 2 user user 4096 May 6 14:45 Documents drwxr-xr-x 2 user user 4096 May 6 14:45 
Downloads drwxr-xr-x 2 user user 4096 May 6 14:45 Music drwxr-xr-x 2 user user 4096 
May 6 14:45 Pictures drwxr-xr-x 2 user user 4096 May 6 14:45 Public drwxr-xr-x 2 
user user 4096 May 6 14:45 Templates drwxr-xr-x 2 user user 4096 May 6 14:45 Videos

In this example, we’re using the ls -l command to list the contents of a directory in long format. We’re then piping that output into the column command and using the -t flag to format the output into columns. As you can see, the resulting output is much easier to read than the original output of ls -l.

Specifying the Delimiter

As I mentioned earlier, the column command will use any whitespace character as the default delimiter between columns. However, you can also specify a delimiter of your choice using the -s flag. Here’s an example −

$ cat example.txt John,Smith,35 Jane,Doe,27 Bob,Johnson,42 $ cat example.txt | column -s , -t John Smith 35 Jane Doe 27 Bob Johnson 42

In this example, we’re using the cat command to output the contents of a file called example.txt. We’re then piping that output into the column command and using the -s flag to specify that the delimiter between columns should be a comma (,). The resulting output is then formatted into columns using the -t flag.

Aligning Columns

By default, the column command will left-align columns. However, you can also use the -o flag to specify an offset for each column. Here’s an example −

$ cat example.txt | column -s , -t -o " | " John | Smith | 35 Jane | Doe | 27 Bob | Johnson | 42

In this example, we’re using the cat command to output the contents of a file called example.txt. We’re then piping that output into the column command and using the -s flag to specify that the delimiter between columns should be a comma (,). We’re also using the -t flag to format the output into columns. Finally, we’re using the -o flag to specify that each column should be offset by a pipe (|) character.

As you can see, the resulting output is much easier to read than the original output of cat example.txt. By aligning the columns, we’ve made it easier to compare values across different rows.

Wrapping Text

By default, the column command will truncate any text that is longer than the width of the column. However, you can use the -c flag to wrap text instead of truncating it. Here’s an example −

$ cat longtext.txt | column -c 80 Lorem ipsum dolor sit amet, consectetur 
adipiscing elit. Praesent ut metus quis enim pellentesque varius. Nulla facilisi. 
Nullam luctus, risus vel mattis placerat, nunc arcu cursus sapien, in bibendum 
felis risus quis metus. Maecenas lacinia leo sapien, eget bibendum mi rhoncus a. In 
hac habitasse platea dictumst. Fusce sit amet arcu et diam pretium faucibus. Donec 
laoreet magna sed dui consectetur lobortis. Ut ultrices ante id est congue, eu 
tincidunt mauris hendrerit. Duis at fringilla velit.

In this example, we’re using the cat command to output the contents of a file called longtext.txt. We’re then piping that output into the column command and using the -c flag to wrap the text at 80 characters. As you can see, the resulting output is much easier to read than the original output of cat longtext.txt.

Sorting Columns

The column command can also be used to sort columns of data. To do this, you can use the sort command in conjunction with column. Here’s an example −

$ cat numbers.txt 4 2 3 1 5 7 6 8 9 $ cat numbers.txt | column -t | sort -n -k 2 4 2 3 1 5 7 6 8 9

In this example, we’re using the cat command to output the contents of a file called numbers.txt. We’re then piping that output into the column command and using the -t flag to format the output into columns. Finally, we’re using the sort command with the -n flag to sort numerically and the -k flag to specify that we want to sort based on the second column.

As you can see, the resulting output is sorted based on the second column. This can be useful when you have a lot of data and you want to quickly find the highest or lowest values in a particular column.

Conclusion

The column command is a powerful tool for working with text files in Linux. It can help you format text into columns, specify delimiters, align columns, wrap text, and even sort columns of data. By mastering the different ways to use the column command, you can save yourself time and frustration when working with large amounts of data. So next time you find yourself struggling to make sense of a text file, remember to give the column command a try!

Updated on: 26-Jun-2023

698 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements