Hi,
I'm a learner of PERL programming.
I've a input file with the below data:
SWAT_5, 1703, 2010-09-21
SWAT_6, 2345, 2010-09-21
SWAT_7, 1792, 2010-09-21
SWAT_8, 1662, 2010-09-21
SWAT_9, 1888, 2010-09-21
VXHARP_1, 171, 2010-09-21
I need to sort this data based on the second numeric field value and want to display only first two fields. Below is expected result:
VXHARP_1, 171
SWAT_8, 1662
SWAT_5, 1703
SWAT_7, 1792
SWAT_9, 1888
SWAT_6, 2345
I wrote the below script to accomplish this:
open( INFILE, "< $input_file") or die "cannot open input file $input_file: $!";
my @lines = <INFILE>;
close(INFILE);
@lines = sort @lines;
foreach my $line (@lines) {
my @tokens = split(/,/, $line);
my $id = $tokens[0];
my $duration = $tokens[1];
printf "$id, $duration\n";
}
But, I'm getting first field sorted output as below:
SWAT_5, 1703
SWAT_6, 2345
SWAT_7, 1792
SWAT_8, 1662
SWAT_9, 1888
VXHARP_1, 171
Please suggest me for my above code correction to sort based on second field.
Thanks and Regards / Mysore 101 Ganapati.