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
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 esc033 %
ascii a141 %
#!/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
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
a stores a
in the pat shell variable; the sed substitute command becomes:
where stands for a space and
nnn stands for the octal value of the entry for