Next: , Previous: Print bash environment, Up: Examples


4.5 行の文字を反転する

以下のスクリプトは,行の文字の位置を反転するために使用することが可能で す.二つの文字を同時に移動するテクニックで,直観的な実装より高速になり ます.

ラベル定義の前のtxコマンドに注意してください.これはtコマ ンドでテストされるフラグをリセットするために必要になることがよくありま す.

想像力豊かな読者は,このスクリプトの使い方が分かるでしょう.例えば, bannerの出力を反転させることです1

     #!/usr/bin/sed -f
     
     /../! b
     
     # Reverse a line.  Begin embedding the line between two newlines
     s/^.*$/\
     &\
     /
     
     # Move first character at the end.  The regexp matches until
     # there are zero or one characters between the markers
     tx
     :x
     s/\(\n.\)\(.*\)\(.\n\)/\3\2\1/
     tx
     
     # Remove the newline markers
     s/\n//g

Footnotes

[1] これは,banner の出力を埋める他のスクリプトが必要です.例えば以下のようにします.

     #! /bin/sh
     
     banner -w $1 $2 $3 $4 |
       sed -e :a -e '/^.\{0,'$1'\}$/ { s/$/ /; ba; }' |
       ~/sedscripts/reverseline.sed