home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 51.2 How UNIX Keeps Time Chapter 51
Miscellaneous Useful Programs and Curiosities
Next: 51.4 Who's On?

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

% ascii a

% ascii \&


Remember to escape special characters (8.19 ) 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
   exit 1
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:


  a |/nnn


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

Previous: 51.2 How UNIX Keeps Time UNIX Power Tools Next: 51.4 Who's On?
51.2 How UNIX Keeps Time Book Index 51.4 Who's On?

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System