Perl sort different from unix sort

Discussion in 'Perl Misc' started by Jose Luis, Mar 3, 2011.

  1. Jose Luis

    Jose Luis Guest

    Hi,

    How can I get the same sort for "foo.txt" in perl and unix sort?


    <<snip>>

    indra@bhsciences:/tmp$ cat foo.txt
    1 2
    10 1
    indra@bhsciences:/tmp$ cat sort.pl

    open my $fh, shift;

    my @s1 = <$fh>;
    print sort @s1;

    indra@bhsciences:/tmp$ perl sort.pl foo.txt
    1 2
    10 1
    indra@bhsciences:/tmp$ sort foo.txt
    10 1
    1 2
    indra@bhsciences:/tmp$ uname -a
    Linux bhsciences 2.6.32 #3 SMP Tue May 11 10:48:46 CEST 2010 x86_64 GNU/Linux


    <<snip>>

    Thanks in advance,
    Jose Luis
     
    Jose Luis, Mar 3, 2011
    #1
    1. Advertising

  2. On 03/03/11 16:52, Jose Luis wrote:
    > Hi,
    >
    > How can I get the same sort for "foo.txt" in perl and unix sort?


    Sort numerically with -n?

    $ sort -n foo.txt
    1 2
    10 1
    -----------------8<---------------
    > indra@bhsciences:/tmp$ perl sort.pl foo.txt
    > 1 2
    > 10 1
    > indra@bhsciences:/tmp$ sort foo.txt
    > 10 1
    > 1 2

    -----------------8<---------------
     
    Charlie Harvey, Mar 3, 2011
    #2
    1. Advertising

  3. >>>>> "Jose" == Jose Luis <> writes:

    Jose> indra@bhsciences:/tmp$ perl sort.pl foo.txt
    Jose> 1 2
    Jose> 10 1
    Jose> indra@bhsciences:/tmp$ sort foo.txt
    Jose> 10 1
    Jose> 1 2
    Jose> indra@bhsciences:/tmp$ uname -a
    Jose> Linux bhsciences 2.6.32 #3 SMP Tue May 11 10:48:46 CEST 2010
    Jose> x86_64 GNU/Linux

    Works fine on FreeBSD. Maybe Linux broke sort.

    print "Just another Perl hacker,"; # the original

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
     
    Randal L. Schwartz, Mar 3, 2011
    #3
  4. Jose Luis

    Steve C Guest

    On 03/03/2011 11:52 AM, Jose Luis wrote:
    > Hi,
    >
    > How can I get the same sort for "foo.txt" in perl and unix sort?
    >
    >
    > <<snip>>
    >
    > indra@bhsciences:/tmp$ cat foo.txt
    > 1 2
    > 10 1
    > indra@bhsciences:/tmp$ cat sort.pl
    >
    > open my $fh, shift;
    >
    > my @s1 =<$fh>;
    > print sort @s1;
    >
    > indra@bhsciences:/tmp$ perl sort.pl foo.txt
    > 1 2
    > 10 1
    > indra@bhsciences:/tmp$ sort foo.txt
    > 10 1
    > 1 2
    > indra@bhsciences:/tmp$ uname -a
    > Linux bhsciences 2.6.32 #3 SMP Tue May 11 10:48:46 CEST 2010 x86_64 GNU/Linux
    >
    >
    > <<snip>>


    UNIX sort:
    If no flags are specified, the sort command sorts entire lines of the input file
    based upon the collation order of the current locale.

    Perl sort:
    If SUBNAME or BLOCK is omitted, "sort"s in standard string comparison order.
    When "use locale" is in effect, "sort LIST" sorts LIST according to the current
    collation locale. See perl-locale.
     
    Steve C, Mar 3, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,839
  2. Navin
    Replies:
    1
    Views:
    744
    Ken Schaefer
    Sep 9, 2003
  3. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    301
    Michele Dondi
    Jan 22, 2004
  4. colin_lyse
    Replies:
    1
    Views:
    163
    Tore Aursand
    Feb 3, 2005
  5. Replies:
    1
    Views:
    143
    Sherm Pendley
    Oct 6, 2006
Loading...

Share This Page