Display Command Output or File Contents in Column Format in Linux

Sometimes there may be too many columns crammed into a single file. That makes it difficult to read the content of the file and point out which data belongs to which column. In order to have a better view, we ca use certain commands that will allocate space between the columns and also mark some separation characters that will make it clear to see the beginning and end of the column.

Sample File

Lets’ look at the below sample file which we will use to demonstrate the column command. We can get the file from kaggle.here.

$ cat iris.data

Running the above code gives us the following result −


Applying the column Command

The column command makes the layout of the column very clear. It uses the –t and –s switches. The -t helps to determine the number of columns the input contains and creates a table and the -s specifies a delimiter character.

$ cat iris.data | column -t -s ","

Running the above code gives us the following result −

SepalLengthCm       SepalWidthCm       PetalLengthCm       PetalWidthCm       Species
5.1                   3.5                1.4                   0.2          Iris-setosa
4.9                   3.0                1.4                   0.2          Iris-setosa
4.7                   3.2                1.3                   0.2          Iris-setosa
4.6                   3.1                1.5                   0.2          Iris-setosa
5.0                   3.6                1.4                   0.2          Iris-setosa
5.4                   3.9                1.7                   0.4          Iris-setosa
4.6                   3.4                1.4                   0.3          Iris-setosa
5.0                   3.4                1.5                   0.2          Iris-setosa
4.4                   2.9                1.4                   0.2          Iris-setosa
4.9                   3.1                1.5                   0.1          Iris-setosa
5.4                   3.7                1.5                   0.2          Iris-setosa
4.8                   3.4                1.6                   0.2          Iris-setosa
4.8                   3.0                1.4                   0.1          Iris-setosa

Mount Command

Another example is the mount command which is most often used by unix admins. The original result is not clearly legible, but we can make it columnar and nicely formatted.

$ mount

Running the above code gives us the following result −

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)

Then we runt he below command which gives us a formatted output.

$ mount | column –t

Next we run it with the mount command.

sysfs       on /sys             type    sysfs
proc       on /proc             type    proc
udev       on /dev             type    devtmpfs
devpts    on /dev/pts          type    devpts
tmpfs    on /run                type    tmpfs
/dev/sda1       on /             type    ext4