Raw Input
Sed is a stream editor.
Sed is a stream editor.
Sed is a stream editor.
A stream editor is used to perform
basic text transformations on an input stream
basic text transformations on an input stream
(a file or input from a pipeline).
Desired Output
Sed is a stream editor.
A stream editor is used to perform
basic text transformations on an input stream
(a file or input from a pipeline).
Script and Comments
Script1
[ 1] $!N
[ 2] /^([^\n]*)\n\1$/D
[ 3] P
[ 4] D
Comments
  1. A line will be printed only if
    • it is different from the next line, or
    • it is the last line of the file.
    What this script does is comparing the current line with the next one, and printing it if any of the above two conditions is satisfied.
  2. The `-r' option of GNU sed must be used or you have to escape every parenthesis used in Step [2].
  3. The Pattern Space is abbreviated to `PS'.
  4. Step [1] is used to append the next line to PS.
  5. After Step [1], there will be two lines in PS:
    • If two lines are the same, command `D' of Step [2] will discard the first line, then make sed jump to Step [1];
    • Otherwise, the first line of PS will be printed by Step [3], and Step [4] will delete it and make sed jump to Step [1].
Script2
[ 1] $!N
[ 2] /^([^\n]*)\n\1$/!P
[ 3] D
Comments
  1. A neat version.