How to create readable output using Linux commands

The command line interface is much more dense than the equivalent GUI on Windows. With one instruction, you can get a screen full of data, with columns, calculations and colors. Most of the commands have additional options that allow you to modify your output to get the exact information you are looking for.

Unfortunately, this power comes with a loss of convenience. For example, many useful commands, such as netstat, generate their output in a series of non-fixed-width columns, making it difficult to parse by human readability standards. Even simple commands like listing the contents of a directory can contain additional information to confuse you.

In this article, we’ll show you how to do three things:

  1. Choose to show only certain columns of the output
  2. Retain color options for specific commands
  3. Make columns neat

1. Selecting display on specific columns

The simple ls command has options to display a lot more data than just the file and directory name. For example, this command:

ls -l

The ‘-l’ option contains a list of columns for each entry, such as permissions, who owns it, creation time, etc., as shown here:

Useful as there may be too much of this data. Fortunately, Linux has a system of “pipelines” for outputting commands to others, and using them, we can choose which columns to display using the “awk” command.

Suppose in the screenshot here we only want to display the last column – the name of the file or folder. We do it like this:

ls -l | awk '{print $ 9}'

This generates the following output:

How to create readable output using Linux commands

The key here is part of the command that says:

awk '{print $ 9}'

Which only displays column number 9. We take the output from ls -l and pipe it to awk. If we want, we can display columns 1 and 9 like this:

ls -l | awk '{print $ 1, $ 9}'

You might be thinking that it is too much effort to type all of the above every time you want to print multiple columns. You can even forget it! And you are right here. This is why you can simply create your own custom command to automatically run it with a keyword of your choice!

2. Saving color information for ls

Since ls is a common command, you will probably use it with awk. You may have noticed in the previous examples that awk’s output destroys ls’s color information. This is not a good thing because many of us rely on color coding to quickly distinguish between folders, directories, and files.

With ls, we can store this information using the “-color = always” option. So our awk command above:

ls -l --color = always | awk '{print $ 9}'

This command produces the following:

How to create readable output using Linux commands

Correctly?

3. Displaying variable width columns in a table

The ls command is very helpful. Its output is neat and each column has a specific width. But what if you have a file where the columns are not aligned? For example, take this file:

cat animal-count

How to create readable output using Linux commands

Since each animal name has a different width, the next column starts in a different place each time. In a long list, the output may become unreadable. Commands like netstat cannot be decoded.

As before, we have a command called “column” into which we can plug in the output so that it displays the data in a table. Here is:

cat animal-count | column -t

The “-t” parameter specifies a table. We get the following output:

How to create readable output using Linux commands

It’s much easier to read. We often use “column” when parsing the output of commands with a large number of data columns and variable widths. This is especially useful for log files that sometimes contain thousands of lines of code.

If you liked this post, share it with your friends via social media, or if you have any questions regarding this blog post, please leave a comment below and we will reply as soon as possible. Thank!

Related Posts