#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $header='u_ci_id,u_display_name,name,fqdn,ip_address,mac_address'; my $field = $ARGV[0] || 'u_ci_id'; my $input_xml = do { local $/; }; my $twig = new XML::Twig; $twig->xparse($input_xml); my $root = $twig->root(); my @data = (); my @cols = split ',',$header; unshift @cols, $field; # added for sort for my $record ($root->children) { push @data,[ map { $record->first_child($_)->text } @cols]; } my @sorted = sort { $b->[0] cmp $a->[0] } @data; print $header."\n"; for (@sorted){ print join ',',@$_[1..$#cols]; # exclude sort col print "\n"; } __DATA__ fqdnB ip1 display1 name1 mac5 id 1 fqdnA ip2 display2 name2 mac4 id 2