Sometimes the output of Linux commands displayed on terminal, are difficult to understand due to their non-intuitive layout. This may also happen if you try to view certain file content on terminal. One of the simplest ways to solve this problem is to organize the terminal output in a column format so that you can easily understand the information displayed on your screen. In this article, we will learn how to display command output & file content in column format in Linux.
Display Command Output & File Content in Column Format
We will use column command for this purpose that allows you to easily display information in column format. It is already installed on most Linux systems by default. You can pass output of Linux commands, input file as well as streams to this command and it will properly reformat their information as columns before displaying on the screen. Let us say you have the following file data.txt that contains list of 5 authors in pipe delimited format.
$ cat data.txt pos|author|articles|comments 1|john|431|9785 2|jim|369|7894 3|joe|194|2349 4|cassey|172|3256 5|jane|165|2378
As you can see, the file contains information in pipe delimited format which looks congested. Here is the command to display the output for this file in column format.
$ cat data.txt | column -t -s "|"
In the above command, we use cat command to output the file content. But we pipe it to column command which will parse and reformat it as columns. For this purpose, we use -t option to automatically determine the total number of columns in the input and create a table accordingly. We also use -s option to specify our delimiter. Based on these options, column command will parse the input data into columns.
$ cat data.txt | column -t -s "|" pos author articles comments 1 john 431 9785 2 jim 369 7894 3 joe 194 2349 4 cassey 172 3256 5 jane 165 2378
Please note, the rows are filled before the columns, in the above command. If you want to the columns to be filled before rows, you need to use -x option. Also, if you need column command to consider empty lines, use -e option.
You can also store the output of column command in another file for later use, with the help of redirection operator ‘>’. Here is an example to store the above command’s output in another file, column_data.txt.
$ cat data.txt | column -t -s "|" > column_data.txt $ cat column_data.txt pos author articles comments 1 john 431 9785 2 jim 369 7894 3 joe 194 2349 4 cassey 172 3256 5 jane 165 2378
Certain Linux commands already display output in column formats. If you want to customize it, or make it more legible, you can also pass their output to column command. For example, mount command already displays output in column format. Here is a command to pass its output to column command and display it as a table.
$ mount $ mount | column -t sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) udev on /dev type devtmpfs (rw,nosuid,relatime,size=4013172k,nr_inodes=1003293,mode=755) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) ...
For more information about column command, refer to its documentation.
$ man column
In this article, we have learnt how to format command output & file contents in column format. column is a very nifty command that you can use along with other commands and file contents to easily parse the information into column format. You can also use it in shell scripts, in case you want to automate data parsing.
Also read:
How to Create Nested Directory in Python
How to Add Blank Directory in Git Repository
How to Iterate Through Files in Directory in Linux
How to Find All Text Files in Directory in Python
How to Convert CSV to Tab Delimited File in Python