UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 35.7 offset: Indent Text Chapter 35
You Can't Quite Call This Editing
Next: 35.9 Splitting Files at Fixed Points: split
 

35.8 Centering Lines in a File

Here's an awk script, written by Greg Ubben, that centers lines across an 80-character line. If your system understands #! (44.4, 45.3), this script will be passed directly to awk without a shell. Otherwise, put this into a Bourne shell script (44.14):

#!/usr/bin/awk -f
{
    printf "%" int(40+length($0)/2) "s\n", $0
}

For each input line, the script builds a printf command with a width specification (like %widths) that's just wide enough to center the line.

In vi, you can use a filter-through (30.22) command to center lines while you're editing. Or just use center from the command line. For example:

% center afile > afile.centered
% sort party_list | center | lp

- JP


Previous: 35.7 offset: Indent Text UNIX Power ToolsNext: 35.9 Splitting Files at Fixed Points: split
35.7 offset: Indent Text Book Index35.9 Splitting Files at Fixed Points: split

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