col Command in Linux



The col command in Linux is a handy tool for formatting text output, especially when dealing with data that might have strange spacing or line breaks. It primarily functions by taking input from standard input (usually a pipe from another command) or a file and arranging it into neat columns for better readability on your terminal.

The col command reads text data and interprets various control characters, such as spaces, tabs, newlines, and carriage returns. It intelligently formats the input into columns, ensuring each line wraps at appropriate points without distorting the content. It can also handle some special formatting characters used by tools like nroff and tbl to create tables or formatted text.

Table of Contents

Here is a comprehensive tutorial to understand the various options available with the col command in linux −

Understanding col Command

Understanding these options allows you to customize how col formats your text output, ensuring a more readable and visually appealing presentation on your terminal.

The col primarily focuses on handling control characters related to spacing and line movement, including space, backspace, tab, newline, carriage return, shift-in/out, and vertical tab. Unrecognized control characters and escape sequences are usually discarded by col.

Prerequisite: Install col Command

The col command is likely already installed on your Linux system. It's a common tool included in the util-linux or bsd extrautils package (formerly bsdmainutils).

Here's how to check and install it if needed. To check if col is installed, open your terminal and type −

col --help
Install col Command

If you get a "command not found" error, then you need to install it. The package manager for installing col depends in your Linux distribution −

For Debian-based systems (Ubuntu, Mint, etc.)

sudo apt update
sudo apt install bsdextrautils

For RedHat-based systems (Fedora, CentOS, etc.)

sudo dnf update
sudo dnf install coreutils-column

Once the installation is complete, you can verify it by running col --help option. This should now display the help message for the col command.

How to Use col Command in Linux?

The col command in Linux primarily deals with formatting text for proper display, particularly when working with output from tools like nroff and tbl. Here's a breakdown of its options −

Here's a detailed explanation of the options available for the col command −

Options Descriptions
-b, --no-backspaces Disables backspaces in the output. Instead of erasing characters, it simply prints the last character written to each column position.
-f, --fine Allows "half-forward line feeds." Normally, characters meant for a half-line boundary are printed on the following line. This option forces them to stay on the current line.
-h, --tabs Replaces multiple spaces with tab characters for a more compact output.
-l number, --lines number Buffers at least "number" lines in memory before processing. By default, it buffers 128 lines. This can be helpful for handling larger inputs.
-p, --pass Forces unknown control sequences to be passed through unchanged. Usually, col filters out unrecognized control characters, but with this option, they'll be included in the output.
-x, --no-tabs Disables tab characters and replaces them with multiple spaces for scenarios where tabs might not be interpreted correctly.
-t This option is used internally by col to determine the number of columns in the input for proper formatting. It's not typically used directly on the command line.

The col command is a versatile utility in Linux that is used to filter out reverse line feeds from the input, making the output more orderly by only allowing forward and half forward line feeds.

Examples of col Command in Linux

Here are some examples of how the col command can be used in linux −

Formatting output from another command

Let's say you want to view the output of the ps aux command (displays running processes) in a more organized way with a clear separation between columns −

ps aux | col
Formatting output from another command

This pipes the output of ps aux through col, which arranges the information into multiple columns based on spaces and tab characters.

Cleaning unevenly spaced text files

Suppose you have a text file named "data.txt" with lines that don't break consistently and have a mix of spaces and tabs −

Line 1  with extra spaces 
Line2    with tabs
This is line3
Cleaning unevenly spaced text files

You can use col to format it for better readability −

col data.txt

This displays the content of "data.txt" in columns, ensuring each line wraps appropriately without losing information.

Basic Formatting with Tabs

This col command reads from data.txt, replaces multiple spaces with tabs (-h), and displays the formatted output −

sudo col -h < data.txt
Basic Formatting with Tabs

Truncating Long Lines and Defining Output Width

You have a file with lines that exceed your terminal width, causing them to wrap awkwardly. The -x option converts tabs to spaces for better readability on some terminals. Also, users can set the maximum output width to characters (adjust this value as needed for your terminal) −

col -x < data.txt
Truncating Long Lines Output Width

This truncates lines that are longer than characters, ensuring a more manageable display.

Customizing Column Delimiters

You want to format a comma-separated values (CSV) file with a different delimiter (e.g., colon). -s ":" defines a colon (":") as the column delimiter instead of the default comma. This allows you to split the file based on colons and display the data in columns.

col -s ":" < data.csv

Handling Larger Inputs and Controlling Newlines

You have a file with extra empty lines that disrupt the formatting. The “l” option buffers at least "number" lines in memory before processing. By default, it buffers 128 lines −

col -l 5 < data.txt
Handling Larger Inputs and Controlling Newlines

This can be helpful for enhancing readability by adding space between data entries. This can be helpful for handling larger inputs.

Preserving Unknown Escape Sequences (Use with Caution)

You have a file with specific formatting codes that col might not recognize. The option -p forces col to pass through unknown control sequences unchanged.

col removes unrecognized control characters and escape sequences by default. You can use the -p, --pass option to force col to pass unknown control sequences through unchanged, though this might affect formatting −

col -p < data.txt  # Use with caution!
Preserving Unknown Escape Sequences

Caution − Use this option very carefully as it could lead to unexpected output if you're not familiar with the escape sequences involved. It can potentially mess up your formatting instead of preserving it.

Reordering Columns

You have a file where the order of columns needs to be rearranged. Unfortunately, the col command itself doesn't directly support reordering columns. However, you can combine it with other tools like awk or cut to achieve this. Command (using awk) −

col -f < data.txt | awk '{print $2, $1}'  # Print column 2, then column 1
Reordering Columns

Command (using cut) −

col -f < data.txt | cut -d ' ' -f2,1  # Cut column 2, then column 1 using space as delimiter
Command using cut

Removing Backspaces

The -b option disables backspaces in the output. Instead of erasing characters, col simply prints the last written character to each column position. The -b option of the col command can be used to ignore backspaces in the output −

sudo col -b > data.txt
Removing Backspaces

This is useful when you want to print only the last character written to each column position.

Allowing Half Line Feeds

This -f, --fine option allows "half-forward line feeds." This might be used for specific formatting scenarios related to superscripts or subscripts. With the -f option, col permits forward half-line feeds, which is known as "fine" mode −

man cat | col -f > data.txt
Allowing Half Line Feeds

Note − Normally, characters printed on a half-line boundary are printed on the following line.

Disabling Tab Replacement

This tells col to replace multiple spaces with tab characters, usually resulting in a more compact presentation. The -h option prevents the col command from replacing multiple spaces with tabs.

man cp | col -h > data.txt
Disabling Tab Replacement

Without Backspaces, Keeping Half-Line Feeds

Suppresses backspaces (-b) and allows half-line feed characters (-f) to be printed as intended, potentially leading to slightly different line breaks −

sudo col -b -f < data.txt
Without Backspaces Keeping Half-Line Feeds

Passing Through Unknown Control Sequences

By using the -p option, unknown control sequences are passed through unchanged, which is not the default behavior of col. This example instructs col to leave all control sequences unfiltered (-p) −

sudo col -p < data.txt  # Use with caution!
Passing Through Unknown Control Sequences 1

Note − Exercise caution with this option as it can lead to unintended formatting if the control sequences are not understood.

These examples demonstrate the flexibility of the col command in handling different types of formatting issues that can arise when dealing with text in a terminal −

col -V
Passing Through Unknown Control Sequences 2

By understanding how col works and its options, you can effectively manipulate text output and present data in a clear and organized way within your Linux environment.

Alternatives of col Command

The col command is a valuable tool for formatting text in Linux, but there are situations where alternatives might be more suitable −

awk Command

Description: awk is a powerful text processing language that allows for much more versatile manipulation of text data. You can use awk to format text, extract specific fields, perform calculations, and filter data based on conditions.

column Command (part of the coreutils package)

Description: The column command (from coreutils) is another option for basic text formatting. It offers similar functionality to col but might have a slightly different syntax and options.

pr Command (part of the coreutils package)

The pr command is primarily used for printing files side-by-side, but it can also be used for some basic formatting tasks similar to col.

Conclusion

The col command in Linux is a utility tool used to filter out reverse line feeds from the input, ensuring that the output is in the correct order with only forward and half-forward line feeds. It's particularly useful when dealing with the output of nroff and tbl, as it can replace multiple whitespace characters with tabs, making the output more readable and structured.

Understanding and utilizing the col command can significantly streamline your workflow when working with Linux command-line tools that produce text output with complex formatting. Whether you're a system administrator, a developer, or just a Linux enthusiast, mastering commands like col can enhance your command-line proficiency.

Advertisements