Bash: How to Align Columns in Output


Often you may want to use Bash to align the columns in the output.

One of the easiest ways to do so is by using the column command with the following syntax:

column -t teams.txt

This particular example will display the columns from the file named teams.txt using a table ( -t ) as the output format.

By default, it’s assumed that the separator between columns is a space.

However, you can use the -s command to specify a different separator.

For example, you could use the following syntax to display the columns from a CSV file in a table:

column -t -s, athletes.csv

By using -s, we are able to specify that the columns in the file are separated by commas.

The following examples show how to use each of these methods in practice.

Example 1: Align Columns of Text File in Output

Suppose we have a file named teams.txt in our current directory that contains information about various basketball teams.

We can use the cat command to view the contents of the file:

We can use the following syntax to align the columns from this file in the output:

column -t teams.txt

The following screenshot shows how to use this syntax in practice:

Bash align columns in output

Notice that this aligns each of the columns from the text file.

Example 2: Align Columns of CSV File in Output

Suppose we have a file named athletes.csv in our current directory that contains information about various basketball players.

We can use the cat command to view the contents of the file:

We can use the following syntax to align the columns from this file in the output:

column -t -s, athletes.csv

The following screenshot shows how to use this syntax in practice:

Bash align columns from CSV file

Notice that the output aligns each of the columns from the CSV file.

Note: Feel free to specify any delimiter that you would like after the -s parameter if your file uses a different delimiter instead.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Count Number of Columns in File
Bash: How to Count Number of Unique Lines in File
Bash: How to Count Number of Characters in String

Leave a Reply