Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a specific sorting 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
the input file contains many lines - example:

Code:
atype:55987:41096:149376:Z:rtype:69376:25728:43648:Z:
atype:103808:48576:55232:E:rtype:139776:82432:57344:Z:dtype:139776:82432:57344:N:
atype:555872:414496:143376:E:rtype:132456:81228:56448:Z:
rtype:103808:24320:79488:VZ:
dtype:311424:132736:178688:VZ:rtype:103680:30848:72832:VZ:atype:103680:30848:72832:E:ntype:139776:82432:57344:N:

Each line can contain one or more (2,3,4,5..) occurences of;

"atype":"anumber":"anumber":"anumber":"text":

There are two types I want to have "first" on each line - one by one accordingly:

"rtype" and after that "atype" (if "atype" exists) - all other types just after them in alphabetical "by type" order.

The expected output result of above input example should be:

Code:
rtype:69376:25728:43648:Z:atype:55987:41096:149376:Z:
rtype:139776:82432:57344:Z:atype:103808:48576:55232:E:dtype:139776:82432:57344:N:
rtype:132456:81228:56448:Z:atype:555872:414496:143376:E:
rtype:103808:24320:79488:VZ:
rtype:103680:30848:72832:VZ:atype:103680:30848:72832:E:dtype:311424:132736:178688:VZ:ntype:139776:82432:57344:N:

thx!
 
Try this:

Perl:
#!/usr/bin/perl -w

while (<>) {
        my @a=split /:/;
        my %data=();
        for (my $i=0; $i<$#a; $i+=5) { $data{$a[$i]} = join ":",@a[($i+1) .. ($i+4)]; }
        print "rtype:$data{rtype}:";
        print "atype:$data{atype}:" if defined($data{atype});
        foreach my $k (sort grep !/^[ra]type/,keys %data) { print "$k:$data{$k}:"; }
        print "\n";
}

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top