Description
Given a line contains several integers (strings matched [0-9]+), we want to enclosed with a pair of brackets every integer except the first and the last one.
Raw Input
begin 147 next 258 another 3690 final 112358 end
keep 14710 keep 25811 end
Desired Output
begin 147 next [258] another [3690] final 112358 end
keep 14710 keep 25811 end
Script and Comments
Script1
[ 1] s/[0-9]+/\n&/g
[ 2] s/\n(.*)\n/\1/
[ 3] s/\n([0-9]+)/[\1]/g
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. Step [2] removes the first and the last newline characters inserted by Step [1] since we are not interested in the first and the last integers.
  4. Step [3] encloses with a pair of angle brackets every integer following a newline character.