Description
  • Given a datafile containing even number of lines:
    • every line with odd line number is a tag;
    • others are data lines.
  • Every tag line is followed by an associated data line.
  • What we want is
    • splitting every data line to several lines by inserting a newline char between every two adjacent characters;
    • then prepend the associated tag to every lines resulting from splitting.
  • Two scripts are provided where the first one much more readable than the second.
Raw Input Desired Output
12
ABC
3456
DEFGH
12A
12B
12C
3456D
3456E
3456F
3456G
3456H
Script and Comments
Script1
[ 1] N
[ 2] :loop
[ 3] s/^\(.*\)\n\(.\)/\1\2\n\1\n/
[ 4] P
[ 5] s/^[^\n]*\n//
[ 6] /\n$/!b loop
[ 7] d
Comments
  1. Given a tag line 12 and the associated dataline ABC, the iterations made by the script to get the desired output is shown in the following:
    Step PS after executing that step
    initial 12
    1 12\nABC
    3, 4 12A\n12\nBC
    5 12\nBC
    6 back to 2 12\nBC
    3, 4 12B\n12\nC
    5 12\nC
    6 back to 2 12\nC
    3, 4 12C\n12\n
    5 12\n
    7 start a new cycle
Script2
[ 1] /\n/!N
[ 2] s/^\(.*\)\n\(.\)/\1\2\n\1\n/
[ 3] P
[ 4] /\n$/!D
[ 5] d
Comments