Next: , Previous: Reverse chars of lines, Up: Examples


4.6 ファイルの行を反転する

以下のものは,様々なUnixコマンドをエミュレートする全く意味がない(面白い けどね)スクリプトです.これは特にtacと同等の動作をします.

GNU sedGNU sed以外の実装では, このスクリプトは簡単に内部バッファでオーバーフローする可能性があること に注意してください.

     #!/usr/bin/sed -nf
     
     # reverse all lines of input, i.e. first line became last, ...
     
     # from the second line, the buffer (which contains all previous lines)
     # is *appended* to current line, so, the order will be reversed
     1! G
     
     # on the last line we're done -- print everything
     $ p
     
     # store everything on the buffer again
     h