col – The Filter Control Character

The col command is a filter that removes backspace characters and performs other control character translations. It is commonly used to filter text files that contain control characters, such as those generated by certain text editors or word processors.

Overview

The col command reads from standard input and writes to standard output. By default, it removes backspace characters and replaces them with spaces. It also removes carriage returns, form feeds, and other control characters.

Here is an example of how to use the col command to filter a file:

$ cat file.txt
Hello, world!
This is a test.
^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H

$ cat file.txt | col
Hello, world!
This is a test.

In this example, the file.txt contains the string “Hello, world!” on the first line, “This is a test.” on the second line, and a series of backspace characters on the third line. When we pipe the contents of file.txt to the col command, the backspace characters are removed, and the resulting output is the original two lines of text.

Options

The col command has the following options:

Option Description
-b Remove backspace characters only.
-f Remove form feed characters only.
-n Do not output the newline character at the end of the output.
-x Remove all control characters except for tab, newline, and form feed.

Here is an example of how to use the -b option to remove backspace characters only:

$ cat file.txt
Hello, world!
This is a test.
^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H

$ cat file.txt | col -b
Hello, world!
This is a test.

In this example, the -b option is used to remove only the backspace characters, leaving the other control characters intact.

Troubleshooting tips

If the col command does not seem to be working as expected, check the input file for control characters that may not be recognized by col. The od command can be used to display the hexadecimal codes for each character in a file:

$ cat file.txt | od -t x1
0000000 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a 54 68
0000020 69 73 20 69 73 20 61 20 74 65 73 74 2e 0a 08 08
0000040 08 08 08 08 08 08 08 08 08 08 08 08 08 08 08 08
0000060 08 08 08 08 0a
0000065

In this example, the od command is used to display the contents of file.txt in hexadecimal format. The backspace characters are displayed as 08, which confirms that they are present in the file.

Notes

The col command is often used in conjunction with other commands in a pipeline to filter control characters from text files. It is a simple and effective way to clean up text files that may contain unwanted control characters.