sed Find and Replace ASCII Control Codes / Nonprintable Characters

Q. How do I find and replace character codes ( control-codes or nonprintable characters ) such as ctrl+a using sed command under UNIX like operating systems?A. ASCII is the American Standard Code for Information Interchange. It is a 7-bit code. Many 8-bit codes (such as ISO 8859-1, the Linux default character set) contain ASCII as their lower half. The international counterpart of ASCII is known as ISO 646.

Character codes often contain code positions which are not assigned to any visible character but reserved for control purposes.

You can easily find and replace them with the help of shell substitute:sed -e 's/'$(echo "octal-value")'/replace-word/g' ORsed 's/'`echo "octal-value"`'/replace-word/g' To replace 0x1B (033 octal) with ASCII letters, enter:

sed's/'`echo"\033"`'/foo/g'

OR

sed -e 's/'$(echo"\033")'/ESC/g'

You can view ascii table by reading its man page or here is the ascii table for your references:

Very important: When using the echo command as shown above, the leading 0 is always required; even when the value is three digits long. So for example: $(echo “357277275”) will work, but $(echo “\357\277\275″) does NOT work.

I learned this the hard way when I changed the 033 in the example to 357 and it broke.