List
The list command (l) displays the contents of the pattern space, showing non-printing characters as two-digit ASCII codes. It is similar in function to the list (:l) command in vi. You can use this command to detect “invisible” characters in the input.[6]
$ cat test/spchar
Here is a string of special characters: ^A ^B
^M ^G
$ sed -n -e "l" test/spchar
Here is a string of special characters: \01 \02
\15 \07
$# test with GNU sed too
$gsed -n -e "l" test/spchar
Here is a string of special characters: \01 \02 \r \a
Because the list command causes immediate output, we suppress the default output or we would get duplicate copies of the lines.
You cannot match a character by ASCII value (nor can you match octal values) in sed.[7] Instead, you have to find a key combination in vi to produce it. Use CTRL-V to quote the character. For instance, you can match an ESC character (^[). Look at the following script:
# list line and replace ^[ with "Escape" l s/^[/Escape/
Here’s a one-line test file:
The Great ^[ is a movie starring Steve McQueen.
Running the script produces the following output:
The Great \33 is a movie starring Steve McQueen. The Great Escape is a movie starring Steve McQueen.
GNU sed produces this:
The Great \1b is a movie starring Steve McQueen. The Great Escape is a movie starring Steve McQueen.
The ^[ character was made in vi by entering CTRL-V, then pressing the ESC key.
Stripping Out Non-Printable Characters from nroff Files
The UNIX formatter nroff produces output for ...
Get sed & awk, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.