Mastering the Linux column Command: A Comprehensive Guide with Examples

The column command in Linux is a powerful yet often overlooked tool for formatting text output into columns. It is particularly useful for making messy or unstructured data more readable by aligning it into neat tables. Whether you’re a system administrator, developer, or just a Linux enthusiast, mastering the column command can save you time and make your work more efficient.
In this article, we’ll explore the column command in detail, covering all its use cases with practical examples. By the end, you’ll be able to use column like a pro!
1. Introduction to the column Command
The column command is part of the util-linux package and is available on most Linux distributions. It is designed to format text into columns, making it easier to read and analyze. It works with both piped input and files, and it supports various options for customizing the output.
2. Basic Usage
The simplest way to use column is to pipe text into it. By default, it formats the input into columns:
$ echo -e "Name Age\nJohn 25\nJane 30\nDoe 22" | column -t
Name Age
John 25
Jane 30
Doe 22
- The
echocommand generates a list of names and ages. - The
columncommand formats the output into two columns.
3. Filling Rows Before Columns (-x)
By default, column fills columns before rows. However, you can use the -x option to fill rows first:
$ echo -e "Name Age\nJohn 25\nJane 30\nDoe 22" | column -x
Name Age John 25 Jane 30 Doe 22
The -x option ensures that rows are filled before columns.
4. Using column with Files
Instead of piping input, you can directly pass a file to column:
$ column file.txt
- This formats the contents of
file.txtinto columns.
5. Formatting find Command Output
The column command is particularly useful for cleaning up the output of commands like find:
$ find /usr/share/fonts -name "*.otf" | column
- This lists all
.otffiles in/usr/share/fontsand formats the output into columns.
6. Table Mode (-t)
The -t option enables table mode, which is ideal for formatting structured data:
$ cat /etc/passwd | column -t -s ":"
- This formats the
/etc/passwdfile into a table, using:as the delimiter.
7. Adding Column Headers (-N)
The -N option allows you to add column headers to the output:
$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER"
- This formats
/etc/passwdinto a table with custom column headers.
8. Hiding Columns (-H)
The -H option allows you to hide specific columns:
$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER" -H "PW"
- This hides the
PW(password) column from the output.
9. Reordering Columns (-O)
The -O option allows you to reorder columns:
$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER" -O "USERNAME,UID,GID,COMMENT,HOME,INTERPRETER"
- This reorders the columns, removing the
PWcolumn.
10. JSON Output (-J)
The -J option outputs the data in JSON format:
$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER" -J -n "users"
- This converts the
/etc/passwddata into a JSON object namedusers.
11. Combining Options for Advanced Formatting
You can combine multiple options for more advanced formatting:
$ cat /etc/passwd | column -t -s ":" -N "USERNAME,PW,UID,GID,COMMENT,HOME,INTERPRETER" -H "PW" -O "USERNAME,UID,GID,COMMENT,HOME,INTERPRETER" -J -n "users"
This command:
- Formats
/etc/passwdinto a table. - Adds column headers.
- Hides the
PWcolumn. - Reorders the columns.
- Outputs the result as JSON.
12. Conclusion
The column command is a versatile tool for formatting text output into columns, making it easier to read and analyze. Whether you’re working with command output or files, column can help you present data in a clean, tabular format.
Key Takeaways:
- Use
-tfor table mode. - Use
-sto specify a delimiter. - Use
-Nto add column headers. - Use
-Hto hide columns. - Use
-Oto reorder columns. - Use
-Jfor JSON output.
For more advanced data manipulation, consider using tools like awk. However, for quick and easy formatting, column is an indispensable tool in your Linux toolkit.
Final Thoughts
The column command is a simple yet powerful way to organize and present data. By mastering its options, you can save time and make your work more efficient. Whether you’re a system administrator, developer, or just a Linux enthusiast, column is a tool you’ll want to keep handy.
Happy formatting! 🚀
