Have you ever found yourself needing to process CSV files and generate output in a structured table format? Not too long ago, I was dealing with a messy file that wasn't properly formatted. It had excessive spaces between each column, and I needed to convert it into CSV format for database upload. After cleaning it up and generating the CSV output, I found it difficult to visually verify the data's integrity because the formatting wasn't very clear. That’s when the column command became extremely useful for me.
According to the manpage, the column command is for "columnating lists." In simpler terms, it's a lightweight utility that allows you to format your data into a structured table format—organizing it into rows and columns—based on how your source file is laid out. The column command is part of the util-linux package.
An important thing to keep in mind is that the column command behaves differently on Debian-based and RHEL-based distributions. This is because Debian-based systems use the column utility from the bsdmainutils package instead of the one from util-linux. The version in util-linux is more up-to-date compared to the one in bsdmainutils. You can read more about this in the bug report.
<code>$ dpkg -S $(which column)</code>
For this demonstration, I'm using CentOS 7, and I’ll show you the differences between Ubuntu and CentOS 7. To check the version of the column command (and also the version of util-linux), run the following command. Note that this won’t work on Debian or Ubuntu systems.
<code>$ column --version # does not work on Debian/ubuntu</code>
Alternatively, you can check the util-linux version using these commands:
<code>$ rpm -qa | grep -i util-linux # for Redhat, Centos, Fedora, Amazon Linux $ dpkg -l | grep -i util-linux # for Ubuntu</code>
Before diving into the column command, it's a good idea to check the man page to explore all available options.
<code>$ man column</code>
Display File Content in Table Format
You can use the column command to convert a file into a table by passing the filename as an argument along with the -t
option. For example, I'll use the /etc/passwd file as input.
<code>$ column -t /etc/passwd</code>
Looking at the image above, the output might not be what you expected—it may look odd. This is because column uses space as the default delimiter. This behavior can be changed by specifying a custom delimiter.
Using a Custom Delimiter
Custom delimiters offer more flexibility. Use the -s
option followed by your desired delimiter. Let’s use ":"
as the delimiter to format the /etc/passwd file properly.
<code>$ column -s ":" -t /etc/passwd</code>
As you can see, the output is now neatly formatted. Starting from util-linux version 2.23, the -s
option no longer treats multiple adjacent delimiters as one.
However, if you run the same command on Ubuntu, you'll notice that it does treat multiple delimiters as one. This is due to the version of column included in bsdmainutils.
<code>$ column -s ":" -t /etc/passwd</code>
To fix this behavior on Debian or Ubuntu, use the -n
flag.
<code>$ column -t -s ":" -n /etc/passwd # Only on Debian/Ubuntu</code>
Skip or Include Empty Lines
By default, the column command ignores blank lines in the input file. Let’s say your input file is in CSV format and contains blank lines between entries. When you format it using column, those blank lines will be skipped.
<code>$ column -t -s ";" dummy.txt</code>
As shown in the image above, empty lines in dummy.txt are ignored when generating the table.
Note: This behavior is consistent across both bsdmainutils and util-linux versions of the column command. However, on Debian or Ubuntu, you can override this by using the -e
flag.
<code>$ column -e -t -s "," dummy.txt # Only on Debian/Ubuntu</code>
Now the table is generated correctly, and the empty lines are preserved.
Customize Output Separator
By default, two spaces are used as the separator between columns. You can change this by using the -o
option. However, this feature is only available in the util-linux version of column (used in RHEL-based systems).
<code>$ column -t -s "," -o "||" dummy.txt # Only on Rhel based distro</code>
Transpose Rows into Columns
Using the -x
option, you can transpose rows into columns. This feature works the same way in both RHEL and Ubuntu variants of the column command. This can be particularly useful when you need to extract certain fields and make them headers in your CSV file.
<code>$ column -x fillcols.txt</code>
Alternatively, if you run the column command without any flags, it behaves the same as if you had used the -x
flag.
Determine Terminal Column Width
The column command uses the ($COLUMNS)
environment variable to determine the width of your terminal. Based on that, it adjusts how the table is displayed.
<code>$ echo $COLUMNS</code>
In the example below, I resized my terminal window so that $COLUMNS
was set to 60, and then I ran the column command. I then resized the window again so that $COLUMNS
was set to 114, and ran the same command again. You can clearly see the difference in how the table is displayed based on terminal size.
<code>$ column -t -s ":" /etc/passwd | head 5</code>
That wraps up this guide. If you have any suggestions or feedback, feel free to leave a comment below.
The above is the detailed content of Different Ways to Use Column Command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

If you find that the server is running slowly or the memory usage is too high, you should check the cause before operating. First, you need to check the system resource usage, use top, htop, free-h, iostat, ss-antp and other commands to check CPU, memory, disk I/O and network connections; secondly, analyze specific process problems, and track the behavior of high-occupancy processes through tools such as ps, jstack, strace; then check logs and monitoring data, view OOM records, exception requests, slow queries and other clues; finally, targeted processing is carried out based on common reasons such as memory leaks, connection pool exhaustion, cache failure storms, and timing task conflicts, optimize code logic, set up a timeout retry mechanism, add current limit fuses, and regularly pressure measurement and evaluation resources.

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

Frankly speaking, I cannot recall the last time I used a PC with a CD/DVD drive. This is thanks to the ever-evolving tech industry which has seen optical disks replaced by USB drives and other smaller and compact storage media that offer more storage

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

Built on Chrome’s V8 engine, Node.JS is an open-source, event-driven JavaScript runtime environment crafted for building scalable applications and backend APIs. NodeJS is known for being lightweight and efficient due to its non-blocking I/O model and

Data replication is the process of copying your data across multiple servers to improve data availability and enhance the reliability and performance of an application. In MySQL replication, data is copied from a database from the master server to ot
