Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Display Command Output or File Contents in Column Format in Linux
When working with files containing multiple columns, data can appear cramped and difficult to read. The column command in Linux helps format text into readable columns with proper spacing and alignment, making data analysis much easier.
Sample File
Let's examine a sample CSV file with iris dataset that we'll use to demonstrate the column command ?
$ cat iris.data
The output shows cramped CSV data ?
Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species 1,5.1,3.5,1.4,0.2,Iris-setosa 2,4.9,3.0,1.4,0.2,Iris-setosa 3,4.7,3.2,1.3,0.2,Iris-setosa 4,4.6,3.1,1.5,0.2,Iris-setosa 5,5.0,3.6,1.4,0.2,Iris-setosa 6,5.4,3.9,1.7,0.4,Iris-setosa 7,4.6,3.4,1.4,0.3,Iris-setosa ........... ........
Using the column Command
The column command formats data into readable columns. The -t flag creates a table format, while -s specifies the delimiter character ?
$ cat iris.data | column -t -s ","
The formatted output shows clearly separated columns ?
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species 1 5.1 3.5 1.4 0.2 Iris-setosa 2 4.9 3.0 1.4 0.2 Iris-setosa 3 4.7 3.2 1.3 0.2 Iris-setosa 4 4.6 3.1 1.5 0.2 Iris-setosa 5 5.0 3.6 1.4 0.2 Iris-setosa 6 5.4 3.9 1.7 0.4 Iris-setosa 7 4.6 3.4 1.4 0.3 Iris-setosa 8 5.0 3.4 1.5 0.2 Iris-setosa 9 4.4 2.9 1.4 0.2 Iris-setosa 10 4.9 3.1 1.5 0.1 Iris-setosa 11 5.4 3.7 1.5 0.2 Iris-setosa 12 4.8 3.4 1.6 0.2 Iris-setosa 13 4.8 3.0 1.4 0.1 Iris-setosa
Formatting mount Command Output
The mount command output is often difficult to read. Let's see the original unformatted output ?
$ mount
The cramped output makes it hard to distinguish mount points ?
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) udev on /dev type devtmpfs (rw,nosuid,relatime,size=1977472k,nr_inodes=494368,mode=755) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=401592k,mode=755) /dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered) securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k) tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755) cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd)
Applying column Formatting
Using column -t creates properly aligned columns ?
$ mount | column -t
The formatted output clearly separates filesystem, mount point, and type ?
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) udev on /dev type devtmpfs (rw,nosuid,relatime,size=1977472k,nr_inodes=494368,mode=755) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=401592k,mode=755) /dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
Common Options
| Option | Description | Example |
|---|---|---|
-t |
Create table format with aligned columns | column -t file.txt |
-s "char" |
Specify delimiter character | column -t -s "," file.csv |
-c width |
Set output width | column -c 80 file.txt |
Conclusion
The column command transforms cramped text data into readable, properly aligned columns. Use -t for table formatting and -s to specify delimiters for CSV files.
