#!/bin/sh -
../ed - <<\EOT
r \s1.d
s/\([^ ][^ ]*\)/(\1)/g
2s
/3/s
/\(4\)/sr
/\(.\)/srg
%s/i/&e/
w \s1.o
EOT