Description
Given a line contains several integers (strings matched [0-9]+), we want to enclosed with a pair of brackets every integer except the last one.
Raw Input
begin 147 next 258 another 3690 final 112358 end
Desired Output
begin [147] next [258] another [3690] final 112358 end
Script and Comments
Script1
[ 1] s/[0-9]+/\n&/g
[ 2] :loop
[ 3] s/\n([0-9]+)([^\n]+\n)/[\1]\2/g
[ 4] t loop
[ 5] s/\n//
Comments
  1. The `-r' option of GNU sed must be used to make sed interpret REs as EREs.
  2. Step [1] is used to insert a newline character before every integer.
  3. Steps [2] thru [4] constitute a loop which will do the replacement, where each substitution encloses an integer only when there exists a newline character behind it:
    Initially
    begin \n147 next \n258 another \n3690 final \n112358 end
    After 1st subs of 1st iteration
    begin [147] next \n258 another \n3690 final \n112358 end
    begin [147] next \n258 another \n3690 final \n112358 end
    After 2nd subs of 1st iteration
    begin [147] next \n258 another [3690] final \n112358 end
    begin [147] next \n258 another [3690] final \n112358 end
    After 1st subs of 2nd iteration
    begin [147] next [258] another [3690] final \n112358 end
Script2
[ 1] s/[0-9]+/\n&/g
[ 2] s/\n([^\n]*)$/\1/
[ 3] s/\n([0-9]+)/[\1]/g
Comments
  1. Step [1] is used to insert a newline character before every integer.
  2. Step [2] removes the last newline character since we are not interested in the last integer.
  3. Step [3] encloses with a pair of angle brackets every integer following a newline character.