Formatting Text into Columns
Now that you understand the basics of the column
command, let's dive deeper into how to use it to format text into columns.
Specifying the Column Delimiter
By default, the column
command uses whitespace (spaces and tabs) as the delimiter to separate the columns. However, you can specify a different delimiter using the -t
option:
$ echo "Name,Age,Gender" | column -t -s,
Name Age Gender
In this example, the -s,
option tells column
to use the comma (,
) as the column delimiter.
Adjusting Column Width
The column
command automatically adjusts the width of each column to fit the longest entry in that column. However, you can manually control the column width using the -o
option:
$ echo "Name Age Gender" | column -t -o 10
Name Age Gender
In this example, the -o 10
option sets the width of each column to 10 characters.
Handling Multi-Line Entries
The column
command can also handle multi-line entries. When an entry spans multiple lines, the command will ensure that the entry is displayed in a single column:
$ cat <<EOF | column -t
Name Age Gender
John Doe 25 Male
Jane Doe 30 Female
EOF
Name Age Gender
John Doe 25 Male
Jane Doe 30 Female
In this example, the input data contains multi-line entries, and the column
command preserves the formatting.
Aligning Columns
By default, the column
command aligns the columns to the left. However, you can change the alignment using the -c
option:
$ echo "Name Age Gender" | column -t -c right
Name Age Gender
In this example, the -c right
option aligns the columns to the right.