Description
In the following example, we want to replace every string matched [0-9][0-9]* with a sequence of dashes(`-') of the same length.
Raw Input
first 2468 second 13579 another 112358 final
Desired Output
first ---- second ----- another ------ final
Script and Comments
Script1
[ 1] :begin
[ 2] s/[0-9][0-9]*/\n&\n/
[ 3] /\n/!b
[ 4] :loop
[ 5] s/\n./-\n/
[ 6] /\n\n/!t loop
[ 7] s/\n\n//
[ 8] b begin
Comments
  1. Step [2] is used to enclose the first matched string with a pair of newline characters.
  2. If there are no matched strings, substitution of Step [2] will fail, no newline characters will be inserted into the Pattern Space.
    In this case, command `b' of Step [3] will make sed jump to the end of the script, print the current line, and start a new cycle.
  3. Otherwise, the loop consisting of steps [5] thru [7] will replace every character between the pair of newline characters with a dash.
  4. The following table demonstrates how the script converts the first matched string `2468' to `----':
    first 2468 second
    first \n2468\n second
    first -\n468\n second
    first --\n68\n second
    first ---\n8\n second
    first ----\n\n second