Display Command Output or File Contents in Column Format in Linux

LinuxOperating SystemFile System

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.

Example

$ cat iris.data

Output

Running the above code gives us the following result −

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
……………
………….

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.

Example

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

Output

Running the above code gives us the following result −

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.

Example

$ mount

Output

Running the above code gives us the following result −

Then we run the below command which gives us a formatted output.

Example

$ mount | column –t

Output

Next we run it with the mount command.

raja
Published on 14-Feb-2020 10:20:24
Advertisements