Description
  • Given a string consists of three parts separated by hyphens.
  • The second part consists of digits only.
  • The job is to insert dots to divide the second part, from right to left, to substrings each consisting of three digits.
Raw Input Desired Output
XXA-192168000254-XXB
XXCC-1401130171542-XXD
XE-21102110213795-XF
XXA-192.168.000.254-XXB
XXCC-1.401.130.171.542-XXD
XE-21.102.110.213.795-XF
Script and Comments
Script1
[ 1] :loop
[ 2] s/([0-9])([0-9]{3}(\.[0-9]{3})*-[^-]*)$/\1.\2/
[ 3] t loop
Comments
  1. The '-r' option of GNU sed must be used to make sed interpret REs as EREs.
  2. Steps [1] thru [3] constitute a loop.
  3. In RE of Step [2]:
    • The second pair of parentheses is used to capture the substring from the tail of the string till the beginning of three digits before which a dot will be inserted in this iteration.
    • The dot will be inserted only when the first digit is preceded by another digit, which is captured by the first pair of parentheses.
    • The third pair is used to group a dot \. and three digits [0-9]{3} as an entity to be used with the asterisk.
  4. If the substitution performed by Step [2] succeeds, command `t'' of Step [3] will make sed branch to Step [1] then [2] to see whether further substitutions possible.