Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, there is a problem while sorting files as per specific fields running on different platforms.......for lunix i can specify the command
system ("sort -n -t , -k 1,3 input_file.txt > ss.txt"); rename (ss.txt, input_file.txt);
where the options and their meaning is as given
-k, --key=POS1[,POS2] start a key at POS1, end it at POS 2 (origin 1) -t, --field-separator=SEP use SEP instead of non- to whitespace transi +tion -n, --numeric-sort compare according to string numerical value
All this is done because i need the entire file to be sorted based on the 1st field only. The various fields are seperated by a space and a comma ( ,)... Is there any perl equivalent of the above code

Edited by Chady -- code tags addition to preserve formatting

Replies are listed 'Best First'.
Re: probems with sorting file in perl
by TheEnigma (Pilgrim) on Aug 30, 2004 at 11:53 UTC
    This should do it, although I'm not sure if reading the input file in one slurp scales well to very large files. You did say the fields are separated by a space and a comma? A comma followed by a space seems more likely; if that's the case the regexes should be changed to
    /^(.*?), /
    If the first field is not numeric on a line, you will get warnings (if they're turned on), but it will finish, and sort them first in the output.

    #!/usr/bin/perl -w use strict; my @whole_file = <>; my @new_array = sort by_first_field @whole_file; print @new_array; sub by_first_field { $a =~ /^(.*?) ,/; my $x = $1; $b =~ /^(.*?) ,/; my $y = $1; return $x <=> $y; }

    This sorts according to the comparison defined in the subroutine "by_first_field". $x and $y are set to the captured data in the regexes, and a return value is generated by using the numeric sort operator <=> (aka the spaceship operator). See chapter 15 in Learning Perl for more info.

    TheEnigma

Re: probems with sorting file in perl
by Limbic~Region (Chancellor) on Aug 30, 2004 at 13:02 UTC
    Anonymous Monk,
    Are you familiar with Perl Power Tools - also known as the Unix Reconstruction Project? You might want to have a gander of a pure perl implementation of sort

    Cheers - L~R

Re: probems with sorting file in perl
by Anonymous Monk on Aug 30, 2004 at 11:27 UTC
    You say you want to sort on the first field only, yet you use -k 1, 3, which is sorting on 3 fields. Anyway, some untested code:
    my $i = 0; open my $fh, "input_file.txt" or die; open my $oh, "> ss.txt" or die; print $oh map {(unpack "NNA*", $_) [-1]} sort map {pack "NNA*", (/(\d+)/) [0], $i ++, $_} <$fh>; close $fh or die; close $oh or die; rename "ss.txt", "input_file.txt" or die;