Many UNIX systems come with a file named
-in a directory
named something like
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 (
) 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
(5) manual pages.
You might also want to make your own version and replace names like
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
search for the octal value of a character.
Remember to escape
special characters (
as I did with that ampersand (
Here's the script:
# 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
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
file (before the blank line that starts the hexadecimal section).
For example, the command
shell variable; the
substitute command becomes:
where stands for a space and
stands for the octal value of the entry for
command also translates a numeric value to the character it represents.
doesn't show non-printable characters by name (like