How to Display Specific Columns in Linux Output Using AWK

How to Display Specific Columns in Linux Output Using AWK

Many Linux commands format their output in a tabular format. This makes it easy to read and understand. However, sometimes there is a littleĀ too much information. If you're SSH'ing from a small screen, the sheer number of columns can spill over into newlines and make it incredibly difficult to understand the output. Or perhaps you just want a cleaner view, or see two particular columns together so you can compare easily.

Whatever the motivation, this tutorial will show you how to extract or cut specific columns from an output, and place them side by side.

Taking a Look at the "ls -l" Command

While the basic "ls" command just prints a list of files and directories to the standard output. But the parameterized "ls -l" shows a much more detailed view of each item - both line and directory. This output is shown in a tabular format as demonstrated here:

Now let's say we want to restrict this output to the first and the last columns. To do this, we need to know the column number of each column we want to cut out. So for example here, the first and last columns are "1" and "9". Now that we know the column numbers, we can display them side by side by piping the output of "ls -l" to another command called "awk" like this:

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

Here, replace "$1" and "$9" with the column numbers that you want to display. With the above, you'll get the following output:

As you can see, the first and last columns are now shown side by side. You can add as many columns as you want in whatever order, by separating them with a dollar sign ($). Note however, that we've lost the color formatting of the original "ls -l" command.

Luckily, there's a workaround for the color with the ls flag "--color=always". So our file command becomes:

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

This gives us the following output:


So now we have the columns we want with the proper color!

Columns without Fixed Widths

The above solution with "awk" works great when you have fixed width columns like with "ls -l". As you can see in the screenshots, the first column has a fixed width and so the second column always starts at the same place. But what if that's not the case? Let's take the netstat command for instance to show which ports are in use.

netstat -tulpn

This is the output:

Here we have the familiar problem with multiple columns spilling over into new lines. Also, this has column headings and each column has variable width. So what if I want to print the 4th and 7th columns? I use "awk" as before:

netstat -tulp | awk '{ print $4,$7 }'

This time however, the output isn't as neat as it was with "ls -l":

This is because the first column "Local" is of a variable width and the next column just starts off where it left off. This has the effect of the output being a complete mess. It's not easy to see the gap between the columns because each starts at a different place. In this output, I want to see which process is using which port. But with this kind of output, it's far from easy!

The solution it turns out, is to further pipe the output of "awk" to a command called "column -t". So the final command becomes:

netstat -tulp | awk '{ print $4,$7 }' | column -t

And now when we look at the output, we get the following:

Display specific columns in Linux

The "column" command, formats the output in a nice regular manner so that it's easy to distinguish them from one another. The labels are also placed properly over each item in the column and now looking at the ports and which process is using them is easy!

So with "awk" and "column -t", you can restrict the columns of any command and ensure that they're displayed properly for comparison.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: We receive a compensation from some of the companies whose products are presented on our website.