sed ': s/[1-9]/&s/g y/123456789/012345678/ s/s0/9s/ t s/0*s\{7\}// /s\{7\}/!s/.*/-1/ :1 s/s/<<123456789s01>/ s/\(.\)<.*\1\(s*.\).*>/\2/ t1'