in reply to difficulty in sorting

What you want is:
my @tosort; while( <DATA> ) { if( m|<address>| .. m|</address>| and not m|</?address>| ) { push @tosort, $_; next } print for sort splice @tosort; print }
Ah, the awk  .. operator.... :-P
[]s, HTH, Massa (κς,πμ,πλ)