sed: uniq
1
1 7.17 Make Duplicate Lines Unique
1 ================================
1
1 This is an example of the art of using the 'N', 'P' and 'D' commands,
1 probably the most difficult to master.
1
1 #!/usr/bin/sed -f
1 h
1
1 :b
1 # On the last line, print and exit
1 $b
1 N
1 /^\(.*\)\n\1$/ {
1 # The two lines are identical. Undo the effect of
1 # the n command.
1 g
1 bb
1 }
1
1 # If the N command had added the last line, print and exit
1 $b
1
1 # The lines are different; print the first and go
1 # back working on the second.
1 P
1 D
1
1 As you can see, we maintain a 2-line window using 'P' and 'D'. This
1 technique is often used in advanced 'sed' scripts.
1