Sort tags in an xml file

Hi All,

Below is an extract from xml. Here the pattern of the tags is not uniform.
i.e., For user A --> name,id,add isthe series
For user B --> id,name,add is the series


<name>A<\name>
<id>A1<\id>
<add>A2<\add>
<id>B1<\id>
<name>B<\name>
<add>B2<\add>
<add>C2<\add>
<id>C1<\id>
<name>C<\name>

Now is there any way to sort the tags with respect to the tags for each user.

i.e.,


<name>A<\name>
<id>A1<\id>
<add>A2<\add>
<name>B<\name>
<id>B1<\id>
<add>B2<\add>
<name>C<\name>
<add>C2<\add>
<id>C1<\id>

Is it possible to do this?
Kindly advice me..

The sort command cannot exactly sort on a field with different > and < delimiters and variable field length, but the following produces a consistent order:

sort -t '>' -k2 file

or

sort -t '>' -k2r file

---------- Post updated at 03:42 PM ---------- Previous update was at 03:34 PM ----------

The following does exactly what you want.
sed changes the first > to a <
then sort works on a < delimited field
then sed changes the (2nd) < back to a >

sed 's/>/</' file | sort -t '<' -k3,3 | sed 's/</>/2'

Hi.

The utility msort allows multiple field delimiters:

#!/usr/bin/env bash

# @(#) s1	Demonstrate multiple field delimiters, msort.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C msort

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
msort -l -q -j -un -d"<>" -n2 -c "lexicographic" $FILE

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
msort 8.44

-----
 Input data file data1:
<name>A<\name>
<id>A1<\id>
<add>A2<\add>
<id>B1<\id>
<name>B<\name>
<add>B2<\add>
<add>C2<\add>
<id>C1<\id>
<name>C<\name>

-----
 Results:
<name>A<\name>
<id>A1<\id>
<add>A2<\add>
<name>B<\name>
<id>B1<\id>
<add>B2<\add>
<name>C<\name>
<id>C1<\id>
<add>C2<\add>

See option descriptions at msort(1) [debian man page]

This has the advantage of passing over the file only once, but the sort itself is slower than GNU sort.

Best wishes ... cheers, drl