How to use the command 'column' (with examples)
The ‘column’ command is used to format the output of a command or a file into multiple columns. By default, the columns are filled before the rows, and the separator is a whitespace. It is a useful tool for creating visually appealing tabular displays.
Use case 1: Format the output of a command for a 30 characters wide display
Code:
printf "header1 header2\nbar foo\n" | column --output-width 30
Motivation: Sometimes, the output of a command can be too wide to fit in the terminal or text editor. By using the --output-width
option, we can limit the output to a specific width and make it more readable.
Explanation:
--output-width 30
: Specifies the width of the output. In this case, it is set to 30 characters.
Example output:
header1 header2
bar foo
Use case 2: Split columns automatically and auto-align them in a tabular format
Code:
printf "header1 header2\nbar foo\n" | column --table
Motivation: When presenting data in a tabular format, it is important to have evenly spaced columns. The --table
option automatically splits the columns and aligns them, creating a visually appealing table.
Explanation:
--table
: Enables tabular formatting, which automatically splits the columns and aligns them.
Example output:
header1 header2
bar foo
Use case 3: Specify the column delimiter character for the --table
option
Code:
printf "header1,header2\nbar,foo\n" | column --table --separator ,
Motivation: In some cases, the input data may be in a different format, such as CSV, where the columns are separated by a different character. By using the --separator
option, we can specify the column delimiter to correctly split and format the columns.
Explanation:
--separator ,
: Specifies the column delimiter character. In this case, it is set to “,”.
Example output:
header1 header2
bar foo
Use case 4: Fill rows before filling columns
Code:
printf "header1\nbar\nfoobar\n" | column --output-width 30 --fillrows
Motivation: By default, the ‘column’ command fills the columns before filling the rows, which can lead to uneven row lengths. However, with the --fillrows
option, we can fill the rows before filling the columns, resulting in a more balanced and visually appealing output.
Explanation:
--fillrows
: Fills the rows before filling the columns, ensuring a balanced output.
Example output:
header1
bar
foobar
Conclusion
The column
command is a versatile tool for formatting output into multiple columns. It provides various options to control the width, alignment, and delimiter used in the formatting. Whether you need to visually organize data or simply adjust the display width, the column
command can help you achieve a neat and well-formatted output.