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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 35.6 Low-Level File Butchery with dd Chapter 35
You Can't Quite Call This Editing
Next: 35.8 Centering Lines in a File
 

35.7 offset: Indent Text

Do you have a printer that starts each line too close to the left margin? You might want to indent text to make it look better on the screen or a printed page. Here's a shell script that does that. It reads from files or standard input and writes to standard output. The default indentation is 5 spaces. For example, to send a copy of a file named graph to the lp printer, indented 12 spaces:

% 

offset -12 graph | lp

There are easier ways to do this (with awk ( 33.11 ) , for instance). This script uses the Bourne shell case statement in an interesting way though, and that might give you ideas for other work.


#! /bin/sh

# GET INDENTATION (IF ANY) AND CHECK FOR BOGUS NUMBERS:
case "$1" in
-[0-9]|-[0-9][0-9]) indent="$1"; shift ;;
-*) echo "`basename $0`: '$1' isn't -number or is > 99." 1>&2; exit 1 ;;
esac

# SET DEFAULT:
case "$indent" in
"") indent=-5 ;;
esac

# BUILD THE SPACES FOR sed.
# FIRST case DOES MULTIPLES OF 10; SECOND case DOES SINGLE SPACES:
s="          "  # TEN SPACES
case "$indent" in
-?) ;;  # LESS THAN 10; SKIP IT
-1?) pad="$s" ;;
-2?) pad="$s$s" ;;
-3?) pad="$s$s$s" ;;
-4?) pad="$s$s$s$s" ;;
-5?) pad="$s$s$s$s$s" ;;
-6?) pad="$s$s$s$s$s$s" ;;
-7?) pad="$s$s$s$s$s$s$s" ;;
-8?) pad="$s$s$s$s$s$s$s$s" ;;
-9?) pad="$s$s$s$s$s$s$s$s$s" ;;
*)  echo "`basename $0`: Help! \$indent is '$indent'!?!" 1>&2; exit 1 ;;
esac

case "$indent" in
-0|-?0) ;;  # SKIP IT; IT'S A MULTIPLE OF 10
-1|-?1) pad="$pad " ;;
-2|-?2) pad="$pad  " ;;
-3|-?3) pad="$pad   " ;;
-4|-?4) pad="$pad    " ;;
-5|-?5) pad="$pad     " ;;
-6|-?6) pad="$pad      " ;;
-7|-?7) pad="$pad       " ;;
-8|-?8) pad="$pad        " ;;
-9|-?9) pad="$pad         " ;;
*)  echo "`basename $0`: Help! \$indent is '$indent'!?!" 1>&2; exit 1 ;;
esac

# MIGHT ADD expand FIRST TO TAKE CARE OF TABS:
sed "s/^/$pad/" $*

First, the script sets the indentation amount, like -12 or -5 , in the indent variable. Next, it builds a shell variable, pad , with just enough spaces to indent the text. One case checks the first digit of $indent to find out how many ten-space chunks of spaces to put in pad . The next case finishes the job with a few more spaces. A sed ( 34.24 ) command adds the spaces to the start of each line. If your lines have TABs in them, change the last line to use expand or pr -e -t ( 41.4 ) and pipe the result to sed :

expand $* | sed "s/^/$pad/"

- JP


Previous: 35.6 Low-Level File Butchery with dd UNIX Power Tools Next: 35.8 Centering Lines in a File
35.6 Low-Level File Butchery with dd Book Index 35.8 Centering Lines in a File

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