Description
Given a data file, in which
  • every line consists of several fields, assume 7 in this case.
  • the delimiter used is '|'.
  • if a line contains less than 6 delimiters, we assume that there exists null field(s) without delimiter(s) at the end of the line.
The job is adding delimiters to make these null fields clear.
Raw Input
G725||144235|N|N|Y|16221
A742|Y|392176|N|N
B329|N|653014|Y|Y|N
Z320|Y|563912|N
Q109
Desired Output
G725||144235|N|N|Y|16221
A742|Y|392176|N|N||
B329|N|653014|Y|Y|N| 
Z320|Y|563912|N||| 
Q109||||||
Script and Comments
Script1
[ 1] s/$/||||||/
[ 2] s/^(([^|]*\|){6}[^|]*).*/\1/
Comments
  1. To reduce annoying backslashes, we use Extended REs in this script,
    be sure to use the '-r' option of sed.
  2. Pattern Space is abbreviated to PS.
  3. Step [1] is used to append 6 delimiters to the end of PS.
  4. There are two pairs of parentheses in Step [2]:
    • The second pair is used to group the RE [^|]*\|
      (zero or more non-| character followed by a '|',
      since '|' is an operator in ERE, we backslash it to make it literal),
      then that RE is combined with the repetition operator {6}.
    • After Step [1], each line contains at least 7 fields where first 7 of them will be caught by the first pair of parentheses of Step [2].
      We have to keep them (\1) and remove redundant '|'s in the REPLACEMENT of s/// command.
Script2
[ 1] s/$/||||||/
[ 2] s/|/\n/7
[ 3] P
[ 4] d
Comments
  1. Step [1] is used to add 6 delimiters to the end of PS.
    After this, PS contains at least 7 delimiters.
  2. Step [2] will replace the 7th delimiter with a newline character.
    After this, PS contains two lines where the first one is what we want, and the second one are redundant delimiters.
  3. Command 'P' of Step [3] is used to print the first line of PS.
  4. Command 'd' of Step [4] will empty PS and start a new cycle.