Linux column Command Tutorial for Beginners (with Examples)

linux column command tutorial for beginners with

Sometimes, while working on the command line in Linux, you might want to display the contents of a file in columnar format. You’ll be glad to know there’s a command line utility in Linux that lets you do this. The tool’s name is column, and we’ll discuss the basics of this command using some easy to understand examples.

But before we do that, it’s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux column command

The column command in Linux lets you columnated lists. Following is its syntax:

column [-entx] [-c columns] [-s sep] [file ...]

And here’s how the tool’s man page defines it:

 The column utility formats its input into multiple columns.  Rows are
     filled before columns.  Input is taken from file operands, or, by
     default, from the standard input.

Following are some Q&A styled examples that should give you a better idea on how the column command works.

Q1. How to use column command?

Basic usage is pretty easy. Suppose you a have file named ‘test.txt’ that contains the following information:

1
2
3
4
5
6
7
8
9
10

Now, to columnate the content of this file, use the column command in the following way:

column test.txt

And you’ll get an output like this:

1    2    3    4    5    6    7    8    9    10

Q2. How to columnate a delimited output?

Suppose a file contains the following contents:

No.|Country|Yes/No
01|India|Y
02|US|Y
03|Australia|Y
04|China|N
05|Russia|Y
06|Japan|Y
07|Singapore|Y
08|South Korea|N
09|Finaland|Y
10|Ireland|Y

Now, run the column command in the following way:

column test.txt -t -s "|"

And here’s the output produced:

No.  Country      Yes/No
01   India        Y
02   US           Y
03   Australia    Y
04   China        N
05   Russia       Y
06   Japan        Y
07   Singpaore    Y
08   South Korea  N
09   Finaland     Y
10   Ireland      Y

FYI, here’s how the column command man page explains the -t and -s command line options:

-s      Specify a set of characters to be used to delimit columns for the
        -t option.

-t      Determine the number of columns the input contains and create a
        table.  Columns are delimited with whitespace, by default, or
        with the characters supplied using the -s option.  Useful for
        pretty-printing displays.

Q3. What about cases with multiple delimiters?

In the previous example, you saw the original content contained pipe ‘|’ as a delimiter. So the column command used this delimiter to produce columnar format output. But what if there were two pipes in some entries. For example, see the first line here:

No.||Country||Yes/No
01|India|Y
02|US|Y
03|Australia|Y
04|China|N
05|Russia|Y
06|Japan|Y
07|Singpaore|Y
08|South Korea|N
09|Finland|Y
10|Ireland|Y

By default, the column command merges multiple adjacent delimiters into a single delimiter. However, if you want, you can use the -n command line option to disable that behavior. So in that case, the column command would become:

column -n test.txt -t -s "|"

Q4. How column command deals with empty lines?

By default, empty lines are ignored by the column command. However, if you want, you can suppress this behavior by using the -e command line option.

For example, file content line this:

No.|Country|Yes/No
01|India|Y
02|US|Y
03|Australia|Y

04|China|N
05|Russia|Y
06|Japan|Y

07|Singapore|Y
08|South Korea|N
09|Finland|Y
10|Ireland|Y

Will come out like:

No.  Country      Yes/No
01   India        Y
02   US           Y
03   Australia    Y

04   China        N
05   Russia       Y
06   Japan        Y

07   Singapore    Y
08   South Korea  N
09   Finland     Y
10   Ireland      Y

using the following command:

column -e test.txt -t -s "|"

Conclusion

Depending on the kind of work you do on the Linux command line, the column command could be of great help to you. In this tutorial, we have discussed majority options offered by the tool. For more info, head to its man page.

نوشته های مشابه