Linux column Command Tutorial for Beginners (with Examples)
0

Typically, whereas engaged on the command line in Linux, you may wish to show the contents of a file in columnar format. You will be glad to know there is a command line utility in Linux that permits you to do that. The device’s identify is column, and we’ll focus on the fundamentals of this command utilizing some simple to know examples.

However earlier than we try this, it is price mentioning that every one examples right here have been examined on an Ubuntu 18.04 LTS machine.

Linux column command

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

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

And this is how the device’s man web page defines it:

     The column utility codecs its enter into a number of columns.  Rows are
     crammed earlier than columns.  Enter is taken from file operands, or, by
     default, from the usual enter.

Following are some Q&A styled examples that ought to provide you with a greater concept on how the column command works.

Q1. Methods to use column command?

Fundamental utilization is fairly simple. Suppose you a have file named ‘check.txt’ that comprises the next info:

1
2
3
4
5
6
7
8
9
10

Now, to columnate the content material of this file, use the column command within the following means:

column check.txt

And you will get an output like this:

1    2    3    4    5    6    7    8    9    10

Q2. Methods to columnate a delimited output?

Suppose a file comprises the next contents:

No.|Nation|Sure/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|Eire|Y

Now, run the column command within the following means:

column check.txt -t -s "|"

And this is the output produced:

No.  Nation      Sure/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   Eire      Y

FYI, this is how the column command man web page explains the -t and -s command line choices:

-s      Specify a set of characters for use to delimit columns for the
        -t choice.

-t      Decide the variety of columns the enter comprises and create a
        desk.  Columns are delimited with whitespace, by default, or
        with the characters equipped utilizing the -s choice.  Helpful for
        pretty-printing shows.

Q3. What about circumstances with a number of delimiters?

Within the earlier instance, you noticed the unique content material contained pipe ‘|’ as a delimiter. So the column command used this delimiter to provide columnar format output. However what if there have been two pipes in some entries. For instance, see the primary line right here:

No.||Nation||Sure/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|Eire|Y

By default, the column command merges a number of adjoining delimiters right into a single delimiter. Nonetheless, if you need, you should utilize the -n command line choice to disable that habits. So in that case, the column command would turn out to be:

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

This autumn. How column command offers with empty strains?

By default, empty strains are ignored by the column command. Nonetheless, if you need, you possibly can suppress this habits through the use of the -e command line choice.

For instance, file content material line this:

No.|Nation|Sure/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|Eire|Y

Will come out like:

No.  Nation      Sure/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   Eire      Y

utilizing the next command:

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

Conclusion

Relying on the type of work you do on the Linux command line, the column command may very well be of nice assist to you. On this tutorial, we’ve mentioned majority choices provided by the device. For more information, head to its man web page.

Microsoft explains one Azure authentication outage as one other one occurs

Previous article

Microsoft Workplace has fairly new icons however they’ve a deadly flaw

Next article

You may also like

Comments

Leave a Reply

More in Linux