> On Mar 24, 2015, at 3:42 AM, Anirban Adhikary <anirban.adhik...@gmail.com> 
> wrote:
> 
> Hi List
> I have a file like this.
> RXMOI:MO=RXOTRX-473-0,SC=0,DCP1=178,SIG=SCCONC,DCP2=179&&186,TEI=0;
> RXMOI:MO=RXOTRX-473-5,SC=0,DCP1=223,SIG=SCCONC,DCP2=224&&231,TEI=5;
> RXMOI:MO=RXOTRX-473-1,SC=0,DCP1=187,SIG=SCCONC,DCP2=188&&195,TEI=1;
...

> This file is from a testing environment. But in production environment this 
> file can be more than 500 lines. So my goal is to sort the file based on the 
> bold numbers and create a new file.
> This is required for data validation. If the file is not in sorted order then 
> it will be very difficult to validate the file manually.

This is a typical application of a technique called a Schwartzian Transform 
(google it). You can combine all of the steps of extracting the keys and 
original line into an anonymous array, sorting the array references, and 
extracting the original line into one command:

#!/usr/bin/perl
use strict;
use warnings;

my @lines = <DATA>;

my @sorted = 
        map { $_->[2] }
        sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] }
        map { [ /RXMOI:MO=RXOTRX-(\d+)-(\d+)/, $_ ] }
        @lines;

print for @sorted;

__DATA__
RXMOI:MO=RXOTRX-473-0,SC=0,DCP1=178,SIG=SCCONC,DCP2=179&&186,TEI=0;
RXMOI:MO=RXOTRX-473-5,SC=0,DCP1=223,SIG=SCCONC,DCP2=224&&231,TEI=5;
RXMOI:MO=RXOTRX-473-1,SC=0,DCP1=187,SIG=SCCONC,DCP2=188&&195,TEI=1;
RXMOI:MO=RXOTRX-1-8,SC=0,DCP1=250,SIG=SCCONC,DCP2=251&&258,TEI=8;
RXMOI:MO=RXOTRX-1-2,SC=0,DCP1=196,SIG=SCCONC,DCP2=197&&204,TEI=2;
RXMOI:MO=RXOTRX-1-0,SC=0,DCP1=178,SIG=SCCONC,DCP2=179&&186,TEI=0;
RXMOI:MO=RXOTRX-460-9,SC=0,DCP1=259,SIG=SCCONC,DCP2=260&&267,TEI=9;
RXMOI:MO=RXOTRX-460-4,SC=0,DCP1=214,SIG=SCCONC,DCP2=215&&222,TEI=4;
RXMOI:MO=RXOTRX-460-8,SC=0,DCP1=250,SIG=SCCONC,DCP2=251&&258,TEI=8;


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to