sed: Centering lines

1 
1 7.2 Centering Lines
1 ===================
1 
1 This script centers all lines of a file on a 80 columns width.  To
1 change that width, the number in '\{...\}' must be replaced, and the
1 number of added spaces also must be changed.
1 
1    Note how the buffer commands are used to separate parts in the
1 regular expressions to be matched--this is a common technique.
1 
1      #!/usr/bin/sed -f
1 
1      # Put 80 spaces in the buffer
1      1 {
1        x
1        s/^$/          /
1        s/^.*$/&&&&&&&&/
1        x
1      }
1 
1      # delete leading and trailing spaces
1      y/tab/ /
1      s/^ *//
1      s/ *$//
1 
1      # add a newline and 80 spaces to end of line
1      G
1 
1      # keep first 81 chars (80 + a newline)
1      s/^\(.\{81\}\).*$/\1/
1 
1      # \2 matches half of the spaces, which are moved to the beginning
1      s/^\(.*\)\n\(.*\)\2/\2\1/
1