ASCII Control Characters: How to Recognize, Find and Safely Handle Them

Learn what the 33 ASCII control characters are, how to spot them in text with caret notation, hex dumps and search patterns, and how to remove only the ones you don't want.

ASCII control characters are the codes in the ASCII table that do not represent letters, digits, punctuation or spaces. Instead, they were designed to control devices and data streams: move to a new line, ring a bell, signal the end of a transmission and so on. Most of them are invisible when text is displayed, which is why they cause confusing problems such as broken CSV imports, failed string comparisons, odd cursor behavior in terminals or parsers rejecting a file that looks perfectly normal. This guide explains what these characters are, how to make them visible, how to locate them precisely, and how to remove unwanted ones without damaging meaningful content like tabs and line breaks.

What counts as an ASCII control character

ASCII defines 128 codes, numbered 0 to 127. Thirty-three of them are control characters: codes 0 through 31 (hexadecimal 00 to 1F) and code 127 (hexadecimal 7F, called DEL). Code 32 is the ordinary space, which is a printable character, not a control character. In Unicode, the same 33 characters keep the same numbers, written as U+0000 to U+001F and U+007F, so everything in this guide applies to UTF-8 text as well as plain ASCII files.

Two notations are useful when reading tool output. Caret notation writes a control character as a caret followed by a printable character: code 1 becomes ^A, code 13 becomes ^M, code 0 becomes ^@ and code 127 becomes ^?. Escape notation, used in many programming languages and command-line tools, writes some common controls as a backslash sequence, such as \t for tab, \n for line feed and \r for carriage return.

Dec | Hex | Abbr | Caret | Common escape | Name
0 | 00 | NUL | ^@ | \0 | Null
1 | 01 | SOH | ^A | — | Start of Heading
2 | 02 | STX | ^B | — | Start of Text
3 | 03 | ETX | ^C | — | End of Text
4 | 04 | EOT | ^D | — | End of Transmission
5 | 05 | ENQ | ^E | — | Enquiry
6 | 06 | ACK | ^F | — | Acknowledge
7 | 07 | BEL | ^G | \a | Bell
8 | 08 | BS | ^H | \b | Backspace
9 | 09 | HT | ^I | \t | Horizontal Tab
10 | 0A | LF | ^J | \n | Line Feed
11 | 0B | VT | ^K | \v | Vertical Tab
12 | 0C | FF | ^L | \f | Form Feed
13 | 0D | CR | ^M | \r | Carriage Return
14 | 0E | SO | ^N | — | Shift Out
15 | 0F | SI | ^O | — | Shift In
16 | 10 | DLE | ^P | — | Data Link Escape
17 | 11 | DC1 | ^Q | — | Device Control 1 (often XON)
18 | 12 | DC2 | ^R | — | Device Control 2
19 | 13 | DC3 | ^S | — | Device Control 3 (often XOFF)
20 | 14 | DC4 | ^T | — | Device Control 4
21 | 15 | NAK | ^U | — | Negative Acknowledge
22 | 16 | SYN | ^V | — | Synchronous Idle
23 | 17 | ETB | ^W | — | End of Transmission Block
24 | 18 | CAN | ^X | — | Cancel
25 | 19 | EM | ^Y | — | End of Medium
26 | 1A | SUB | ^Z | — | Substitute
27 | 1B | ESC | ^[ | \e (non-standard, but widely supported) | Escape
28 | 1C | FS | ^\ | — | File Separator
29 | 1D | GS | ^] | — | Group Separator
30 | 1E | RS | ^^ | — | Record Separator
31 | 1F | US | ^_ | — | Unit Separator
127 | 7F | DEL | ^? | — | Delete

Which control characters are normal and which are suspicious

Not every control character is a problem. Three of them appear in ordinary text all the time, and removing them blindly will damage the file:

  • Tab (HT, 0x09): used for indentation and as the delimiter in tab-separated files.
  • Line feed (LF, 0x0A): the line ending on Linux, macOS and most Unix-like systems.
  • Carriage return (CR, 0x0D): appears before LF in Windows-style line endings (CR LF). A lone CR or a stray CR at the end of lines in a Unix context is often a sign of a file that crossed platforms.

Others deserve attention when they show up in text that is supposed to be human-readable. NUL (0x00) often indicates binary data, a UTF-16 file being read as single bytes, or a truncated or padded field. ESC (0x1B) usually starts a terminal escape sequence, for example color codes copied from terminal output or logs. Backspace (0x08) can come from captured terminal sessions. Form feed (0x0C) and vertical tab (0x0B) occasionally appear in text extracted from documents. SUB (0x1A) has historically been used as an end-of-file marker in some older systems. The remaining codes rarely belong in modern plain text.

Keep in mind that ASCII control characters are only one kind of invisible character. Unicode characters such as the zero-width space, the byte order mark or the non-breaking space are also invisible or hard to distinguish, but they are not ASCII control characters and the patterns below will not catch them. Unicode also defines a second set of control characters, U+0080 to U+009F (the C1 controls), which lie outside ASCII.

Step 1: Make control characters visible in the terminal

The quickest check on Linux and other systems with GNU coreutils is cat with display options. The -v option shows non-printing characters in caret notation (it leaves tab and line feed alone), -T shows tabs as ^I, -E marks each line end with $, and -A combines all three. On BSD and macOS, cat supports -v, -e and -t with similar intent, but flags differ, so check your system's manual page.

cat -A notes.txt

Illustrative example (not captured output): a file saved with Windows line endings and containing a tab and an embedded escape character might display like this:

name^Iscore^M$
alice^I42^M$
^[[31mwarning^[[0m^M$

Reading that example: ^I is a tab, ^M before each $ is a carriage return (so the file uses CR LF line endings), and ^[ is ESC, here starting what looks like terminal color codes. Without -A, those same lines would look like ordinary text, possibly with colors applied.

Step 2: Inspect the exact bytes with a dump tool

Caret notation is convenient, but a byte dump removes any ambiguity about what is in the file. Three common tools do this:

od -c notes.txt      # characters, with escapes like \t \r \n and octal for others
xxd notes.txt        # hex bytes alongside a printable view
hexdump -C notes.txt # canonical hex + ASCII layout

In the hex output, look for byte values 00 to 1F and 7F. In the printable column of xxd and hexdump -C, control characters are typically shown as a dot, which is why the hex column, not the text column, is the authoritative one. od -c is especially readable for this task because it prints the common controls by their escapes and shows the rest as three-digit octal numbers, for example 033 for ESC.

Step 3: Search for unexpected control characters

To find every line containing a control character other than tab, line feed and carriage return, a character-class pattern works well. GNU grep supports this with the -P (Perl-compatible) option; not every grep build includes it. The -n option prints line numbers and -a forces grep to treat the file as text even if it contains NUL bytes, which would otherwise make grep report a binary match.

grep -naP '[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]' notes.txt

The class deliberately skips 0x09 (tab), 0x0A (line feed) and 0x0D (carriage return). If you also want to flag carriage returns, for example in a file that is supposed to use Unix line endings, add \x0D to the class. To count matching lines instead of listing them, add -c.

Step 4: Get exact positions with a short script

When you need line numbers, byte offsets and the precise code for each occurrence, a few lines of Python are portable across operating systems. Reading the file as bytes is safe for UTF-8 text, because UTF-8 never uses bytes below 0x80 inside multi-byte characters, so any byte in the control range really is an ASCII control character. This does not hold for UTF-16 or UTF-32 files, which contain many zero bytes by design; convert those to UTF-8 first. The script below is an illustrative example and has not been run against your data.

import sys

CONTROL = set(range(0x00, 0x20)) | {0x7F}
EXPECTED = {0x09, 0x0A, 0x0D}  # tab, line feed, carriage return

with open(sys.argv[1], "rb") as f:
    data = f.read()

line = 1
for offset, byte in enumerate(data):
    if byte in CONTROL and byte not in EXPECTED:
        print(f"line {line}, byte offset {offset}: 0x{byte:02X}")
    if byte == 0x0A:
        line += 1

Run it as python3 find_controls.py notes.txt. Each output line tells you where a character sits and which code it is, which you can then look up in the table above. For a quick look at a single string inside Python, repr() also helps: it shows control characters as escapes such as \x1b or \t instead of rendering them invisibly.

Step 5: Remove or replace only what you intend to

Before changing anything, keep a copy of the original file. Then decide case by case, because the right fix depends on what the characters mean:

  • Stray control codes with no purpose (for example NUL, SOH or BEL in a text export): delete them.
  • Terminal escape sequences (starting with ESC): deleting only the ESC byte leaves fragments like [31m behind; remove the whole sequence instead, or regenerate the output without colors.
  • Windows line endings in a file that should use Unix endings: convert CR LF to LF rather than deleting every CR, so a lone CR that carries meaning is not silently lost.
  • Tabs that should be spaces: replace them with the intended number of spaces, not with nothing.

For the first case, tr can delete a set of byte values given as octal ranges. The command below removes every ASCII control character except tab (011), line feed (012) and carriage return (015), writing the result to a new file:

tr -d '\000-\010\013\014\016-\037\177' < notes.txt > notes.clean.txt

tr operates on bytes, which is fine for UTF-8 for the same reason given in Step 4, but it is not appropriate for UTF-16 files. If you need to convert line endings, a dedicated converter such as dos2unix, where available, or a targeted substitution of CR LF with LF is safer than a blanket deletion.

Step 6: Verify the result

Confirm both that the unwanted characters are gone and that nothing else changed:

  • Rerun the search from Step 3 on the cleaned file. No output means no unexpected control characters remain.
  • View a sample with cat -A again to check that tabs (^I) and line ends ($, and ^M$ if you kept CR LF) look as intended.
  • Compare the original and cleaned files with diff or a side-by-side comparison tool, and check that only the lines you expected were modified.
  • Compare file sizes: the difference should match the number of bytes you meant to remove. A much larger difference suggests the pattern caught more than intended.

Limits of this guide

No tool-specific documentation or test results were supplied for this article, so the commands describe commonly documented behavior of GNU/Linux utilities and Python rather than verified output on your system. Option names differ between GNU and BSD versions of cat and grep, and grep -P is not available in every build. Check the manual page for the tools you use, and test any cleanup command on a copy before applying it to important data.