Next: , Up: Examples


4.1 行の中央揃え

以下のスクリプトは,ファイルのすべての行を80桁の幅でセンタリングします. 幅を変更するため,\{...\}の数値を変更する必要があり,追加 されるスペースも変更する必要があります.

マッチさせる正規表現の部分を分離するため,バッファコマンドが使用されて いる方法に注意してください — これは一般的なテクニックです.

     #!/usr/bin/sed -f
     
     # Put 80 spaces in the buffer
     1 {
       x
       s/^$/          /
       s/^.*$/&&&&&&&&/
       x
     }
     
     # del leading and trailing spaces
     y/tab/ /
     s/^ *//
     s/ *$//
     
     # add a newline and 80 spaces to end of line
     G
     
     # keep first 81 chars (80 + a newline)
     s/^\(.\{81\}\).*$/\1/
     
     # \2 matches half of the spaces, which are moved to the beginning
     s/^\(.*\)\n\(.*\)\2/\2\1/