Linux column command
columnis a practical command-line tool in Linux systems, used to format input text into multi-column display. It can reorganize originally messy text data into neat table form, greatly improving readability.
Typical Use Cases:
- Format command output (e.g.,
ls、psetc.) - Organize CSV or TSV data
- Create simple terminal tables
- Align log file content
Basic Syntax
column [选项] [文件名]
If no filename is specified,columnreads data from standard input.
Common Options and Parameters
| Option | Description |
|---|---|
-t |
Automatically detect the input delimiter and create a table |
-s sep |
Specify a custom column delimiter (default is whitespace) |
-c width |
Set the total output width (in characters) |
-x |
Fill rows first, then columns (default is columns first, then rows) |
-o sep |
Specify the output column separator (default is two spaces) |
-N names |
Specify names for each column (comma-separated) |
-n |
Do not merge multiple consecutive delimiters |
Usage Examples
Basic Table Formatting
Example
$ echo -e "Name Age GendernAlice 25 FnBob 30 MnCharlie 22 M" | column -t
Name Age Gender
Alice 25 F
Bob 30 M
Charlie 22 M
Name Age Gender
Alice 25 F
Bob 30 M
Charlie 22 M
Handling CSV Data
Example
$ echo -e "ID,Name,Departmentn101,Alice,Salesn102,Bob,IT" | column -s, -t
ID Name Department
101 Alice Sales
102 Bob IT
ID Name Department
101 Alice Sales
102 Bob IT
Custom Output Separator
Example
$ echo -e "1 2 3n4 5 6" | column -t -o " | "
1 | 2 | 3
4 | 5 | 6
1 | 2 | 3
4 | 5 | 6
Combining with Other Commands
Example
$ ls -l | head -5 | column -t
total 48
drwxr-xr-x 2 user user 4096 Jan 10 09:30 Desktop
drwxr-xr-x 2 user user 4096 Jan 10 09:30 Documents
drwxr-xr-x 2 user user 4096 Jan 10 09:30 Downloads
total 48
drwxr-xr-x 2 user user 4096 Jan 10 09:30 Desktop
drwxr-xr-x 2 user user 4096 Jan 10 09:30 Documents
drwxr-xr-x 2 user user 4096 Jan 10 09:30 Downloads
Advanced Usage
Handling Irregular Data
Example
$ echo -e "Item:Price:StocknApple:1.2:50nOrange:0.8:30" | column -s: -t
Item Price Stock
Apple 1.2 50
Orange 0.8 30
Item Price Stock
Apple 1.2 50
Orange 0.8 30
Specifying Column Names
Example
$ echo -e "Alice 25nBob 30" | column -N "Name,Age" -t
Name Age
Alice 25
Bob 30
Name Age
Alice 25
Bob 30
Controlling Output Width
Example
$ echo -e "1 2 3 4 5n6 7 8 9 10" | column -c 30 -t
1 3 5 7 9
2 4 6 8 10
1 3 5 7 9
2 4 6 8 10
Notes
- Delimiter handling: By default,
columntreats multiple consecutive whitespace characters as one delimiter; using the-noption can disable this behavior - Special characters: If the data contains delimiters such as spaces or tabs, it is recommended to explicitly specify the delimiter
- Performance considerations: When processing large files,
columnmay require a lot of memory - Alignment: All columns are left-aligned by default; directly specifying alignment is currently not supported
Comparison of Alternatives
| Tool | Advantages | Disadvantages |
|---|---|---|
column |
Simple and easy to use, built into Linux | Relatively basic functionality |
awk |
Highly flexible and powerful | Complex syntax |
pr |
Supports paging and column control | Complex options |
paste |
Suitable for merging file columns | Limited formatting capability |
Practice Exercises
- Take your
/etc/passwdfile and format the first 5 lines as a table (Tip: use:as the delimiter) - Try using the
columncommand to organize your process list (ps auxoutput) - Create a text file with three columns of data and experiment with different delimiters
-seffect of the option
Example
# Example solution for Exercise 1
head -5 /etc/passwd | column -s: -t
head -5 /etc/passwd | column -s: -t
By mastering thecolumncommand, you can easily convert messy text data into readable table form, which is very practical in daily system administration and data processing.
Other Extensions
Linux Command Encyclopedia