{"id":7376,"date":"2018-11-29T14:56:34","date_gmt":"2018-11-29T11:56:34","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-column-command\/"},"modified":"2018-11-29T14:56:34","modified_gmt":"2018-11-29T11:56:34","slug":"linux-column-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-column-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux column Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<div><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-column-command-tutorial-for-beginners-with-examples.jpg\" class=\"ff-og-image-inserted\" alt=\"\" title=\"\"><\/div>\n<p>Sometimes, while working on the command line in Linux, you might want to display the contents of a file in columnar format. You&#8217;ll be glad to know there&#8217;s a command line utility in Linux that lets you do this. The tool&#8217;s name is column, and we&#8217;ll discuss the basics of this command using some easy to understand examples.<\/p>\n<p>But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-column-command\">Linux column command<\/h2>\n<p>The column command in Linux lets you columnated lists. Following is its syntax:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">column [-entx] [-c columns] [-s sep] [file ...]<\/code><\/pre>\n<p>And here&#8217;s how the tool&#8217;s man page defines it:<\/p>\n<pre><code spellcheck=\"false\"> The column utility formats its input into multiple columns.\u00a0 Rows are<br\/>\u00a0\u00a0\u00a0\u00a0 filled before columns.\u00a0 Input is taken from file operands, or, by<br\/>\u00a0\u00a0\u00a0\u00a0 default, from the standard input.<\/code><\/pre>\n<p>Following are some Q&amp;A styled examples that should give you a better idea on how the column command works.<\/p>\n<h2 id=\"q-how-to-use-column-command\">Q1. How to use column command?<\/h2>\n<p>Basic usage is pretty easy. Suppose you a have file named &#8216;test.txt&#8217; that contains the following information:<\/p>\n<pre><code spellcheck=\"false\">1<br\/>2<br\/>3<br\/>4<br\/>5<br\/>6<br\/>7<br\/>8<br\/>9<br\/>10<\/code><\/pre>\n<p>Now, to columnate the content of this file, use the column command in the following way:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">column test.txt<\/code><\/pre>\n<p>And you&#8217;ll get an output like this:<\/p>\n<pre><code spellcheck=\"false\">1\u00a0\u00a0 \u00a02\u00a0\u00a0 \u00a03\u00a0\u00a0 \u00a04\u00a0\u00a0 \u00a05\u00a0\u00a0 \u00a06\u00a0\u00a0 \u00a07\u00a0\u00a0 \u00a08\u00a0\u00a0 \u00a09\u00a0\u00a0 \u00a010<\/code><\/pre>\n<h2 id=\"q-how-to-columnate-a-delimited-output\">Q2. How to columnate a delimited output?<\/h2>\n<p>Suppose a file contains the following contents:<\/p>\n<pre><code spellcheck=\"false\">No.|Country|Yes\/No<br\/>01|India|Y<br\/>02|US|Y<br\/>03|Australia|Y<br\/>04|China|N<br\/>05|Russia|Y<br\/>06|Japan|Y<br\/>07|Singapore|Y<br\/>08|South Korea|N<br\/>09|Finaland|Y<br\/>10|Ireland|Y<\/code><\/pre>\n<p>Now, run the column command in the following way:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">column test.txt -t -s \"|\"<\/code><\/pre>\n<p>And here&#8217;s the output produced:<\/p>\n<pre><code spellcheck=\"false\">No.\u00a0 Country\u00a0\u00a0\u00a0\u00a0\u00a0 Yes\/No<br\/>01\u00a0\u00a0 India\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Y<br\/>02\u00a0\u00a0 US\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Y<br\/>03\u00a0\u00a0 Australia\u00a0\u00a0\u00a0 Y<br\/>04\u00a0\u00a0 China\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 N<br\/>05\u00a0\u00a0 Russia\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Y<br\/>06\u00a0\u00a0 Japan\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Y<br\/>07\u00a0\u00a0 Singpaore\u00a0\u00a0\u00a0 Y<br\/>08\u00a0\u00a0 South Korea\u00a0 N<br\/>09\u00a0\u00a0 Finaland\u00a0\u00a0\u00a0\u00a0 Y<br\/>10\u00a0\u00a0 Ireland\u00a0\u00a0\u00a0\u00a0\u00a0 Y<\/code><\/pre>\n<p>FYI, here&#8217;s how the column command man page explains the -t and -s command line options:<\/p>\n<pre readability=\"6\"><code spellcheck=\"false\" readability=\"6\">-s\u00a0\u00a0\u00a0\u00a0\u00a0 Specify a set of characters to be used to delimit columns for the<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -t option.<p>-t\u00a0\u00a0\u00a0\u00a0\u00a0 Determine the number of columns the input contains and create a<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 table.\u00a0 Columns are delimited with whitespace, by default, or<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 with the characters supplied using the -s option.\u00a0 Useful for<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pretty-printing displays.<\/p><\/code><\/pre>\n<h2 id=\"q-what-about-cases-with-multiple-delimiters\">Q3. What about cases with multiple delimiters?<\/h2>\n<p>In the previous example, you saw the original content contained pipe &#8216;|&#8217; 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:<\/p>\n<pre><code spellcheck=\"false\">No.||Country||Yes\/No<br\/>01|India|Y<br\/>02|US|Y<br\/>03|Australia|Y<br\/>04|China|N<br\/>05|Russia|Y<br\/>06|Japan|Y<br\/>07|Singpaore|Y<br\/>08|South Korea|N<br\/>09|Finland|Y<br\/>10|Ireland|Y<\/code><\/pre>\n<p>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:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">column -n test.txt -t -s \"|\"<\/code><\/pre>\n<h2 id=\"q-how-column-command-deals-with-empty-lines\">Q4. How column command deals with empty lines?<\/h2>\n<p>By default, empty lines are ignored by the column command. However, if you want, you can\u00a0suppress this behavior by using the -e command line option.<\/p>\n<p>For example, file content line this:<\/p>\n<pre readability=\"5\"><code spellcheck=\"false\" readability=\"4\">No.|Country|Yes\/No<br\/>01|India|Y<br\/>02|US|Y<br\/>03|Australia|Y<p>04|China|N<br\/>05|Russia|Y<br\/>06|Japan|Y<\/p><p>07|Singapore|Y<br\/>08|South Korea|N<br\/>09|Finland|Y<br\/>10|Ireland|Y<\/p><\/code><\/pre>\n<p>Will come out like:<\/p>\n<pre readability=\"5\"><code spellcheck=\"false\" readability=\"4\">No.\u00a0 Country\u00a0\u00a0\u00a0\u00a0\u00a0 Yes\/No<br\/>01\u00a0\u00a0 India\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Y<br\/>02\u00a0\u00a0 US\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Y<br\/>03\u00a0\u00a0 Australia\u00a0\u00a0\u00a0 Y<p>04\u00a0\u00a0 China\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 N<br\/>05\u00a0\u00a0 Russia\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Y<br\/>06\u00a0\u00a0 Japan\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Y<\/p><p>07\u00a0\u00a0 Singapore\u00a0\u00a0\u00a0 Y<br\/>08\u00a0\u00a0 South Korea\u00a0 N<br\/>09\u00a0\u00a0 Finland\u00a0\u00a0\u00a0\u00a0 Y<br\/>10\u00a0\u00a0 Ireland\u00a0\u00a0\u00a0\u00a0\u00a0 Y<\/p><\/code><\/pre>\n<p>using the following command:<\/p>\n<pre class=\"command\"><code spellcheck=\"false\">column -e test.txt -t -s \"|\"<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>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 <a href=\"https:\/\/linux.die.net\/man\/1\/column\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, while working on the command line in Linux, you might want to display the contents of a file in columnar format. You&#8217;ll be glad to know there&#8217;s a command line utility in Linux that lets you do this. The tool&#8217;s name is column, and we&#8217;ll discuss the basics of this command using some easy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7376","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7376"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7376\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}