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 left to right, 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-140.113.017.154.2-XXD
XE-211.021.102.137.95-XF
Script and Comments
Script1
[ 1] :loop
[ 2] s/^([^-]*-([0-9]{3}\.)*[0-9]{3})([0-9])/\1.\3/
[ 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 first pair of parentheses is used to capture the substring from the beginning of the string till the end of three digits after which a dot will be appended in this iteration.
    • The dot will be appended only when the third digit is followed by another digit, which is captured by the third pair of parentheses.
    • The second pair is used to group three digits [0-9]{3} and a dot \. 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.