Americas

  • United States
sandra_henrystocker
Unix Dweeb

Viewing Linux output in columns

How-To
Jun 22, 20176 mins
Data CenterLinux

Government building with greek columns
Credit: Thinkstock

The Linux column command makes it easy to display data in a columnar format — often making it easier to view, digest, or incorporate into a report. While column is a command that’s simple to use, it has some very useful options that are worth considering. In the examples in this post, you will get a feel for how the command works and how you can get it to format data in the most useful ways.

By default, the column command will ignore blanks lines in the input data. When displaying data in multiple columns, it will organize the content by filling the left column first and then moving to the right. For example, a file containing numbers 1 to 12 might be displayed in this order:

1       4       7       10
2       5       8       11
3       6       9       12

A file with some well known lyrics might look like this:

$ cat RR
I've been working on the railroad
All the live long day
I've been working on the railroad
Just to pass the time away

Can't you hear the whistle blowing
Rise up so early in the morn
Can't you hear the whistle blowing
Dinah, blow your horn

Dinah, won't you blow
Dinah, won't you blow
Dinah, won't you blow your horn
Dinah, won't you blow,
Dinah, won't you blow,
Dinah, won't you blow your horn

When we pass the file of lyrics through the column command, we’ll see it in this space-saving format:

$ column RR
I've been working on the railroad       Dinah, blow your horn
All the live long day                   Dinah, won't you blow
I've been working on the railroad       Dinah, won't you blow
Just to pass the time away              Dinah, won't you blow your horn
Can't you hear the whistle blowing      Dinah, won't you blow,
Rise up so early in the morn            Dinah, won't you blow,
Can't you hear the whistle blowing      Dinah, won't you blow your horn

Note that the blank lines we saw in the original file have been removed. If you want the empty lines included in your output, simply use the -e (include empty) option.

$ column -e RR
I've been working on the railroad       Dinah, blow your horn
All the live long day
I've been working on the railroad       Dinah, won't you blow
Just to pass the time away              Dinah, won't you blow
                                        Dinah, won't you blow your horn
Can't you hear the whistle blowing      Dinah, won't you blow,
Rise up so early in the morn            Dinah, won't you blow,
Can't you hear the whistle blowing      Dinah, won't you blow your horn

If you want the data displayed from left to right and then down (row first rather than column first), use the -x option. Think of this as selecting the X axis in a graph.

$ column -x RR
I've been working on the railroad       All the live long day
I've been working on the railroad       Just to pass the time away
Can't you hear the whistle blowing      Rise up so early in the morn
Can't you hear the whistle blowing      Dinah, blow your horn
Dinah won't you blow                    Dinah won't you blow
Dinah, won't you blow your horn         Dinah, won't you blow,
Dinah, won't you blow,                  Dinah, won't you blow your horn

If the fields in your file are separated by commas, colons, or some other character, you can place each field in its own “cell” by specifying the employed delimiter. Say this were the input file:

$ cat abc
a:b:c:d:e

Using the column command with the delimeter specified, we would get this:

$ column -t -s: abc
a  b  c  d  e

In the command below, we send the data using a pipe and get the same effect. The other lines were added to label the two arguments.

     create table -----+   +----- input delimeter 
                       |   |
                       V   V
$ echo a:b:c | column -t -s:
a  b  c

The column command creates as many columns as it has room for in your terminal window. Stretch out your terminal window and you might see more columns. And each column will have the same width regardless of how long the content in any particular column will be. The longest line in your file will determine the column width. If any line takes more than half the width of your terminal window, you’ll only get one column in your entire display.

$ cat poem
The rain
in Spain
falls mainly
on the plain

and that is why
the plain is so
very wet
$ column poem
The rain        falls mainly    and that is why very wet
in Spain        on the plain    the plain is so

If the last three lines were a single line, the output would look like this instead.

$ column poem
The rain
in Spain
falls mainly
on the plain
and that is why the plain is so very wet

On the other hand, you can specify the width of your display (even if it exceeds the width of the actual display) using the -c option. If you specify a ridiculously wide display, your data will wrap around on your display, but it still might be a single line. Here’s what the RR file would look like on a typical terminal window if we tell the command that our window is 1,000 characters wide:

$ column -c 1000 RR
I've been working on the railroad     All the live long day                I
've been working on the railroad      Just to pass the time away           C
an't you hear the whistle blowing     Rise up so early in the morn         C
an't you hear the whistle blowing     Dinah, blow your horn                D
inah won't you blow                   Dinah won't you blow                 D
inah, won't you blow your horn        Dinah, won't you blow,               D
inah, won't you blow,                 Dinah, won't you blow your horn

Yet, it’s easy to show that it’s actually a single line of text:

$ column -c 1000 RR | wc -l
1

If you want a simple list showing how many times each user has logged in recently, you can use the column command in a pipe like this:

$ last | grep pts | awk '{print $1}' | sort | uniq -c | column
     12 jdoe         41 mdoe         25 shs         19 aguy

You can also use the column command in a script to make some potentially useful tables. In the script below, we’re using the column command to create a table with numbers from 1 to 50.

#!/bin/bash

for y in {1..5}
do
  for x in {1..10}
    do echo -n "| $((y*x)) "
  done
  echo
done | column -t
$ ./doit
|  1   |  2   |  3   |  4   |  5   |  6   |  7   |  8   |  9   |  10
|  2   |  4   |  6   |  8   |  10  |  12  |  14  |  16  |  18  |  20
|  3   |  6   |  9   |  12  |  15  |  18  |  21  |  24  |  27  |  30
|  4   |  8   |  12  |  16  |  20  |  24  |  28  |  32  |  36  |  40
|  5   |  10  |  15  |  20  |  25  |  30  |  35  |  40  |  45  |  50

The column command probably isn’t going to make anyone’s top 10 list, but it can save you a lot of time when you might otherwise have to reorganize data by hand.

2-Minute Linux Tip: Learn how to use the crontab command

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author