<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>CLI | Ahmed K Emara</title><link>https://akemara.com/en/tags/cli/</link><atom:link href="https://akemara.com/en/tags/cli/index.xml" rel="self" type="application/rss+xml"/><description>CLI</description><generator>Akemara Kit (https://akemara.com)</generator><language>en</language><lastBuildDate>Wed, 15 Jan 2025 00:00:00 +0000</lastBuildDate><image><url>https://akemara.com/media/logo.svg</url><title>CLI</title><link>https://akemara.com/en/tags/cli/</link></image><item><title>Mastering the Linux column Command: A Comprehensive Guide with Examples</title><link>https://akemara.com/en/blog/linux-column-command/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://akemara.com/en/blog/linux-column-command/</guid><description>&lt;p&gt;The &lt;code&gt;column&lt;/code&gt; command in Linux is a powerful yet often overlooked tool for formatting text output into columns. It is particularly useful for making messy or unstructured data more readable by aligning it into neat tables. Whether you&amp;rsquo;re a system administrator, developer, or just a Linux enthusiast, mastering the &lt;code&gt;column&lt;/code&gt; command can save you time and make your work more efficient.&lt;/p&gt;
&lt;p&gt;In this article, we’ll explore the &lt;code&gt;column&lt;/code&gt; command in detail, covering all its use cases with practical examples. By the end, you&amp;rsquo;ll be able to use &lt;code&gt;column&lt;/code&gt; like a pro!&lt;/p&gt;
&lt;h2 id="1-introduction-to-the-column-command"&gt;1. Introduction to the column Command&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;column&lt;/code&gt; command is part of the &lt;code&gt;util-linux&lt;/code&gt; package and is available on most Linux distributions. It is designed to format text into columns, making it easier to read and analyze. It works with both piped input and files, and it supports various options for customizing the output.&lt;/p&gt;
&lt;h2 id="2-basic-usage"&gt;2. Basic Usage&lt;/h2&gt;
&lt;p&gt;The simplest way to use &lt;code&gt;column&lt;/code&gt; is to pipe text into it. By default, it formats the input into columns:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ &lt;span class="nb"&gt;echo&lt;/span&gt; -e &lt;span class="s2"&gt;&amp;#34;Name Age\nJohn 25\nJane 30\nDoe 22&amp;#34;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; column -t
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Name Age
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;John &lt;span class="m"&gt;25&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Jane &lt;span class="m"&gt;30&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Doe &lt;span class="m"&gt;22&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;echo&lt;/code&gt; command generates a list of names and ages.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;column&lt;/code&gt; command formats the output into two columns.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="3-filling-rows-before-columns--x"&gt;3. Filling Rows Before Columns (-x)&lt;/h2&gt;
&lt;p&gt;By default, &lt;code&gt;column&lt;/code&gt; fills columns before rows. However, you can use the &lt;code&gt;-x&lt;/code&gt; option to fill rows first:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ &lt;span class="nb"&gt;echo&lt;/span&gt; -e &lt;span class="s2"&gt;&amp;#34;Name Age\nJohn 25\nJane 30\nDoe 22&amp;#34;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; column -x
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Name Age John &lt;span class="m"&gt;25&lt;/span&gt; Jane &lt;span class="m"&gt;30&lt;/span&gt; Doe &lt;span class="m"&gt;22&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;-x&lt;/code&gt; option ensures that rows are filled before columns.&lt;/p&gt;
&lt;h2 id="4-using-column-with-files"&gt;4. Using column with Files&lt;/h2&gt;
&lt;p&gt;Instead of piping input, you can directly pass a file to &lt;code&gt;column&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ column file.txt
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;This formats the contents of &lt;code&gt;file.txt&lt;/code&gt; into columns.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="5-formatting-find-command-output"&gt;5. Formatting find Command Output&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;column&lt;/code&gt; command is particularly useful for cleaning up the output of commands like &lt;code&gt;find&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ find /usr/share/fonts -name &lt;span class="s2"&gt;&amp;#34;*.otf&amp;#34;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; column
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;This lists all &lt;code&gt;.otf&lt;/code&gt; files in &lt;code&gt;/usr/share/fonts&lt;/code&gt; and formats the output into columns.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="6-table-mode--t"&gt;6. Table Mode (-t)&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;-t&lt;/code&gt; option enables table mode, which is ideal for formatting structured data:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ cat /etc/passwd &lt;span class="p"&gt;|&lt;/span&gt; column -t -s &lt;span class="s2"&gt;&amp;#34;:&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;This formats the &lt;code&gt;/etc/passwd&lt;/code&gt; file into a table, using &lt;code&gt;:&lt;/code&gt; as the delimiter.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="7-adding-column-headers--n"&gt;7. Adding Column Headers (-N)&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;-N&lt;/code&gt; option allows you to add column headers to the output:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ cat /etc/passwd &lt;span class="p"&gt;|&lt;/span&gt; column -t -s &lt;span class="s2"&gt;&amp;#34;:&amp;#34;&lt;/span&gt; -N &lt;span class="s2"&gt;&amp;#34;USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;This formats &lt;code&gt;/etc/passwd&lt;/code&gt; into a table with custom column headers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="8-hiding-columns--h"&gt;8. Hiding Columns (-H)&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;-H&lt;/code&gt; option allows you to hide specific columns:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ cat /etc/passwd &lt;span class="p"&gt;|&lt;/span&gt; column -t -s &lt;span class="s2"&gt;&amp;#34;:&amp;#34;&lt;/span&gt; -N &lt;span class="s2"&gt;&amp;#34;USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER&amp;#34;&lt;/span&gt; -H &lt;span class="s2"&gt;&amp;#34;PW&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;This hides the &lt;code&gt;PW&lt;/code&gt; (password) column from the output.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="9-reordering-columns--o"&gt;9. Reordering Columns (-O)&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;-O&lt;/code&gt; option allows you to reorder columns:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ cat /etc/passwd &lt;span class="p"&gt;|&lt;/span&gt; column -t -s &lt;span class="s2"&gt;&amp;#34;:&amp;#34;&lt;/span&gt; -N &lt;span class="s2"&gt;&amp;#34;USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER&amp;#34;&lt;/span&gt; -O &lt;span class="s2"&gt;&amp;#34;USERNAME,UID,GID,COMMENT,HOME,INTERPRETER&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;This reorders the columns, removing the &lt;code&gt;PW&lt;/code&gt; column.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="10-json-output--j"&gt;10. JSON Output (-J)&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;-J&lt;/code&gt; option outputs the data in JSON format:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ cat /etc/passwd &lt;span class="p"&gt;|&lt;/span&gt; column -t -s &lt;span class="s2"&gt;&amp;#34;:&amp;#34;&lt;/span&gt; -N &lt;span class="s2"&gt;&amp;#34;USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER&amp;#34;&lt;/span&gt; -J -n &lt;span class="s2"&gt;&amp;#34;users&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;This converts the &lt;code&gt;/etc/passwd&lt;/code&gt; data into a JSON object named &lt;code&gt;users&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="11-combining-options-for-advanced-formatting"&gt;11. Combining Options for Advanced Formatting&lt;/h2&gt;
&lt;p&gt;You can combine multiple options for more advanced formatting:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ cat /etc/passwd &lt;span class="p"&gt;|&lt;/span&gt; column -t -s &lt;span class="s2"&gt;&amp;#34;:&amp;#34;&lt;/span&gt; -N &lt;span class="s2"&gt;&amp;#34;USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER&amp;#34;&lt;/span&gt; -H &lt;span class="s2"&gt;&amp;#34;PW&amp;#34;&lt;/span&gt; -O &lt;span class="s2"&gt;&amp;#34;USERNAME,UID,GID,COMMENT,HOME,INTERPRETER&amp;#34;&lt;/span&gt; -J -n &lt;span class="s2"&gt;&amp;#34;users&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;This command:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Formats &lt;code&gt;/etc/passwd&lt;/code&gt; into a table.&lt;/li&gt;
&lt;li&gt;Adds column headers.&lt;/li&gt;
&lt;li&gt;Hides the &lt;code&gt;PW&lt;/code&gt; column.&lt;/li&gt;
&lt;li&gt;Reorders the columns.&lt;/li&gt;
&lt;li&gt;Outputs the result as JSON.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="12-conclusion"&gt;12. Conclusion&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;column&lt;/code&gt; command is a versatile tool for formatting text output into columns, making it easier to read and analyze. Whether you&amp;rsquo;re working with command output or files, &lt;code&gt;column&lt;/code&gt; can help you present data in a clean, tabular format.&lt;/p&gt;
&lt;h2 id="key-takeaways"&gt;Key Takeaways:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;-t&lt;/code&gt; for table mode.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;-s&lt;/code&gt; to specify a delimiter.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;-N&lt;/code&gt; to add column headers.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;-H&lt;/code&gt; to hide columns.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;-O&lt;/code&gt; to reorder columns.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;-J&lt;/code&gt; for JSON output.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For more advanced data manipulation, consider using tools like &lt;code&gt;awk&lt;/code&gt;. However, for quick and easy formatting, &lt;code&gt;column&lt;/code&gt; is an indispensable tool in your Linux toolkit.&lt;/p&gt;
&lt;h2 id="final-thoughts"&gt;Final Thoughts&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;column&lt;/code&gt; command is a simple yet powerful way to organize and present data. By mastering its options, you can save time and make your work more efficient. Whether you&amp;rsquo;re a system administrator, developer, or just a Linux enthusiast, &lt;code&gt;column&lt;/code&gt; is a tool you&amp;rsquo;ll want to keep handy.&lt;/p&gt;
&lt;p&gt;Happy formatting! 🚀&lt;/p&gt;</description></item></channel></rss>