#!/usr/bin/perl
if(scalar(@ARGV) < 1){
print "Please enter filenames\n";
my $sFilename = <>; chop($sFilename);
@afilename = split(' ',$sFilename);
}
else { @afilename = @ARGV; }
print "Filenames given = @afilename \n";
foreach my $sFile (@afilename){
unless(-r "$sFile"){ print "Can't read/found $sFile\n"; next; }
open FILEHANDLE,"$sFile" or next;
print "Starting to sort $sFile\n";
my @aAllline = <FILEHANDLE>;
foreach(@aAllline){
if(length($_)<3 || $_ =~ /\/\//) { next; }
push(@aLine,$_);
}
my @aSortedline = sort _sortById(@aLine);
unless(-w "$sFile"){ print "Can't writte $sFile.sorted \n"; next; }
open FILEHANDLE2,">$sFile.sorted" or next;
my $sI = 0;
foreach(@aAllline){
if(length($_)<3 || $_ =~ /\/\//) { print FILEHANDLE2 $_; }
else { print FILEHANDLE2 $aSortedline[$sI]; }
$sI++;
}
}
print "End code\n";
sub _sortById {
my @aAline = split('#',$a);
my @aBline = split('#',$b);
# print "a=$a, b=$b\n aline = @aAline \n bline = @aBline \n";
return (($aAline[0] <=> $aBline[0]) or ($aAline[0] cmp $aBline[0]) ); #cmp only ID
}