Description
  • Each line consists of several fields separated by one or more blanks.
  • There are no leading or trailing blanks.
Raw Input Desired Output
1001 one two
1002 three four five
1003 six
1004 seven eight
1001 one 
1001 two
1002 three 
1002 four 
1002 five
1003 six
1004 seven 
1004 eight
Script and Comments
Script1
[ 1] s/^([^ ]+) +[^ ]+ /&\n\1 /
[ 2] P
[ 3] D
Comments
  1. Note that the '-r' option of GNU sed must be used to make it interpret REs as EREs.
  2. Step [1] is used to insert a newline character, the first field, and a blank, at the third field.
  3. After Step [1], the Pattern Space consists of two lines, then:
    • Step [2] is used to print the first line, and
    • Step [3] is used to delete that line, making sed branch to Step [1].
  4. For example, if a line consists of A B C D Three iterations are needed to process that line:
    StepCommandPattern Space
    after command
    0 A B C D
    1s/// A B\nA C D
    2P A B\nA C D
    3D A C D
    0 A C D
    1s/// A C\nA D
    2P A C\nA D
    3D A D
    0 A D
    1s/// A D
    2P A D
    3D A D