Open In App

col command in Linux with Examples

Last Updated : 18 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The col utility of Linux is excellent at stripping any reverse line feeds and instead replacing the whitespace characters with tabs wherever possible. They find it particularly handy when processing output from such commands as `nroff` and `tbl`. The `col` utility reads data from the standard input and writes it to the standard output. Thus, it is a convenient utility while processing text on pipelines.

Syntax

col [-bfhpx] [-l num]

Basic Example

Construct an example file called `sample.txt` with a message including reverse line feeds and whitespace:

Basic Example

sample.txt data

We then run the basic ‘col’ command without options:

cat sample.txt | col > output.txt

This command inputs and removes reverse line feeds, using optimized whitespace.

2024-10-14_14-21

Basic col command example

Key Options of col command

Option

Description

-b

do not output backspaces

-f

allow forward half line feeds

-h

do not output multiple spaces instead of tabs

-l num

buffer at least num lines in memory

-p

pass unknown control sequences unchanged

-x

output multiple spaces instead of tabs

1. -b (No Backspaces)

This option does not show the backspaces, printing instead only the last character written to each column position.

cat sample.txt | col -b > output_no_backspace.txt
-b (No Backspaces)

2. -f (Forward Half Line Feeds)

This option allows half line feeds to be passed through, which may be useful for some formats of text.

cat sample.txt | col -f > output_half_line_feeds.txt
 -f (Forward Half Line Feeds)

3. -h (Do Not Expand Multiple Spaces)

This option will not produce multiple spaces, but instead it will use tabbing where possible:

cat sample.txt | col -h > output_no_multiple_spaces.txt
-h Do Not Expand Multiple Spaces

4. -l (num Buffer Lines)

This option specifies the number of lines to buffer in memory; the default is 128 lines.

cat sample.txt | col -l 256 > output_buffered.txt
-l (num Buffer Lines)

5. -p: Pass Unknown Control Sequences

This option allows unknown control sequences to pass without change, instead of being filtered.

cat sample.txt | col -p > output_pass_control.txt

6. -x: Output Multiple Spaces

This option prints multiple spaces for every tab. This could be necessary for certain types of layout format.

cat sample.txt | col -x > output_multiple_spaces.txt
2024-10-14_14-32

Conclusion

The col command is a general-purpose tool under the Linux text processing toolkit. It is quite indispensable when formatting complex text such as man pages or at any other point of output in different text formatting tools. Further, knowing its different options and usage will enable you to control line feeds, whitespaces, and control characters in your text processing workflows effectively.



Next Article

Similar Reads