次: , 前: tail, 上: Examples


4.14 重複した行を一行にする

以下は,NP,そしてDコマンドを使用した,おそらく マスターするのが最も難しい芸術的な例です.

     #!/usr/bin/sed -f
     h
     
     :b
     # On the last line, print and exit
     $b
     N
     /^\(.*\)\n\1$/ {
         # The two lines are identical.  Undo the effect of
         # the n command.
         g
         bb
     }
     
     # If the N command had added the last line, print and exit
     $b
     
     # The lines are different; print the first and go
     # back working on the second.
     P
     D

御覧のように,PDを使用して二行のウィンドウを管理してい ます.このテクニックは,高度なsedスクリプトでよく使用されます.