[Chapter 51] 51.3 ASCII Characters: Listing and Getting Values
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:57:18Z

#

| Chapter 51 Miscellaneous Useful Programs and Curiosities |

## 51.3 ASCII Characters: Listing and Getting Values

Many UNIX systems come with a file named ascii-in a directory named something like /usr/pub or /usr/share/lib/pub. It's a list of the ASCII character set with the octal and hexadecimal value of each character. Here are two lines from the octal section:

>     |030 can|031 em |032 sub|033 esc|034 fs |035 gs |036 rs |037 us |
>     |040 sp |041  ! |042  " |043  # |044  $ |045  % |046  & |047  ' |

It's saying, for instance, that an escape (`esc`) character has a value of 033 octal, and the percent sign (`%`) is 045 octal.

If your system doesn't have a file like that, you can type one in by hand in a few minutes; start with a printed ASCII chart or the ascii(7) or ascii(5) manual pages. You might also want to make your own version and replace names like `sub` with the key you'd type to get that character (in this case, the two-character representation `^Z`). If your UNIX system doesn't use ASCII, you can make a similar file for it.

Once you have that file, the following shell script, named ascii, can search for the octal value of a character. For example:

>     % ascii esc
>     033
>     % ascii a
>     141
>     % ascii \&
>     046

Remember to escape special characters (8.19) as I did with that ampersand (`&`). Here's the script:

    case

    \(..\)..\1
    #!/bin/sh
    file=/usr/pub/ascii
    # Make pattern with spaces to match field in $file exactly:
    case "$1" in
    [[\\/]) pat=" \\$1 " ;;    # lets us search for [, \ and /
    ?)   pat=" $1 " ;;
    ??)   pat="$1 " ;;
    ???)   pat="$1" ;;
    *)   echo "Usage: `basename $0` char
       (char must be single character like 'a' or name like 'soh')." 1>&2
       exit 1
       ;;
    esac
    sed -n "1,/^\$/s/.*|\([0-9][0-9][0-9]\) $pat|.*/\1/p" $file

The script makes a sed (34.24) expression that exactly matches an entry in the first section of the ascii file (before the blank line that starts the hexadecimal section). For example, the command `ascii` `a` stores a in the pat shell variable; the sed substitute command becomes:

>     s/|nnna|/nnn/p

where stands for a space and `nnn` stands for the octal value of the entry for `a`.

The jot (45.11) command also translates a numeric value to the character it represents. But jot doesn't show non-printable characters by name (like `esc`).

- JP

51.2 How UNIX Keeps Time |  | 51.4 Who's On?