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

How would I sort a file that looks like this? I need for it to sort by the numbers. Im a beginner and yes this should be a fairly easy question to answer. I cant seem to do it.... Thanks in advance. -Taylor
1260896 abadeno 972 achin00 402 astamen 181847 azeleke 134 b005376 1 b005824 I need it to look like this: 1 b005824 134 b005376 402 astamen 972 achin00 181847 azeleke 1260896 abadeno

Replies are listed 'Best First'.
Re: Sorting within a file
by sh1tn (Priest) on Mar 04, 2005 at 20:48 UTC
    UNIX style:
    perl -e 'push @file,$_ while<>;print sort {$a <=> $b} @file' test.txt
    Dos style:
    perl -e "push @file,$_ while<>;print sort {$a <=> $b} @file" test.txt
    Results:
    1 b005824 134 b005376 402 astamen 972 achin00 181847 azeleke 1260896 abadeno


      How about

      perl -e "@file=<>; print sort { $a <=> $b } @file" test.txt
      --------------
      It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
        Or:
        perl -e "print sort {$a <=> $b} <>" test.txt


      Oh, now you've done it, got me started with the golf:

      perl -e 'print sort {$a <=> $b} <>' test.txt

      UNIX style: perl -e 'push @file,$_ while<>;print sort {$a <=> $b} @file' test.txt Dos style: perl -e "push @file,$_ while<>;print sort {$a <=> $b} @file" test.txt Results: 1 b005824 134 b005376 402 astamen 972 achin00 181847 azeleke 1260896 abadeno Is this on the commad line? How would I incorporate this into my scrip +t? Thanks for the help.
        Put this code in your program file:
        use strict; print sort {$a <=> $b} <> __END__ Usage example: sort.pl data.txt


        Surely "Unix-style" would be $ sort -n file.txt?
Re: Sorting within a file
by RazorbladeBidet (Friar) on Mar 04, 2005 at 20:26 UTC
    Try:

    open

    and

    sort

    what have you tried so far?
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: Sorting within a file
by ikegami (Patriarch) on Mar 04, 2005 at 20:34 UTC
    use strict; use warnings; my @lines; { local *FILE; open(FILE, "< filename.in") or die("Couln't open input file: $!\n"); chomp(@lines = <FILE>); } my @sorted_lines = map { $_->[0]} sort { $a->[1] <=> $b->[1] } map { /^(\d+)/ && [ $_, 0+$1 ] } # OR: map { no warnings; [ $_, 0+$1 ] } @lines; { local *FILE; open(FILE, "> filename.out") or die("Couln't open output file: $!\n"); local $, = "\n"; print FILE (@sorted_lines); }

    Update: Fixed a warning.

      my @lines; { local *FILE; open(FILE, "< filename.in") or die("Couln't open input file: $!\n"); chomp(@lines = <FILE>); } my @sorted_lines = map { $_->[0]} sort ( $a->[1] <=> $b->[1] } map { [ $_, 0+$_ ] } @lines; { local *FILE; open(FILE, "> filename.out") or die("Couln't open output file: $!\n"); local $, = "\n"; print FILE (@lines); } Whats with the curly brackets? Here's what I have tried. It looks like + the file is already ordered alphabetically by id when I used "du -ks + *" but I need it by size which is the numbers. Here's my code, it do +esnt seem to change the order. #Push data into an array die("Cannot open Log file to read from.") unless(open(TAKE, "<unix_list.txt")); while ($line = <TAKE>) { push(@list_all, $line); } close(TAKE); @list_all = sort { $a <=> $b } @list_all; die("Cannot open Log file to write to.") unless(open(NEW, ">unix_list.txt")); print NEW @lines; close(NEW);
      Thanks again...
        Whats with the curly brackets?

        It provides a scope for the local *FILE and the local $,. I don't like variables hanging around too long, especially when they have destructors (like FILE) or have side-effects (like $,).

      Its still having problems. Error states "Can't use string ("") as an array ref while 'strict ref' is in use at line "map { /^(\d+)/ && $_, 0+$1 }"
        You must have a typo somewhere???
Re: Sorting within a file
by gopalr (Priest) on Mar 05, 2005 at 04:53 UTC
    while (<DATA>) { if ($_=~m#^([^\s]+)\s+([^\n]+)\n#) { push (@unordered, [$1,$2]); } } @ordered = sort {$a->[0] <=> $b->[0]} @unordered; print "\n$_->[0] = $_->[1]" for @ordered; __DATA__ 1260896 abadeno 972 achin00 402 astamen 181847 azeleke 134 b005376 1 b005824
Re: Sorting within a file
by dmorelli (Scribe) on Mar 04, 2005 at 20:53 UTC
    #! /usr/bin/perl -w use strict; # Read whole file into a list, this could be an open()ed filehandle # instead of the DATA section from below my @lines = <DATA>; # Sort the list by comparing the numeric portion of the lines my @sorted = sort { my ($numA) = $a =~ /^(\d+).*/; my ($numB) = $b =~ /^(\d+).*/; $numA <=> $numB } @lines; # Output the sorted list { # We never chomped, the lines still contain newlines, # so don't need a list separator local $" = ""; print "@sorted"; } __DATA__ 1260896 abadeno 972 achin00 402 astamen 181847 azeleke 134 b005376 1 b005824
Re: Sorting within a file
by trammell (Priest) on Mar 04, 2005 at 21:01 UTC
    $ cat > data 1 b005824 1260896 abadeno 134 b005376 181847 azeleke 402 astamen 972 achin00 $ sort -n data 1 b005824 134 b005376 402 astamen 972 achin00 181847 azeleke 1260896 abadeno
Re: Sorting within a file
by Fletch (Bishop) on Mar 04, 2005 at 21:10 UTC
    puts ARGF.sort_by{|l|l.split(/\s+/)[0].to_i}
Re: Sorting within a file
by Miguel (Friar) on Mar 05, 2005 at 21:06 UTC
    Yet another way, using IO::All and unpack, as your Data looks fixed length delimited:
    #!/usr/bin/perl -w use strict; use IO::All; my @AoA; push @AoA, [ unpack("A7 A8",$_) ] foreach io("file.txt")->getlines; foreach ( sort { $a->[0] <=> $b->[0] } @AoA ) { $_->[1] =~s/^ //; print $_->[0], " ", $_->[1],"\n" }

    Miguel

Re: Sorting within a file
by kscaldef (Pilgrim) on Mar 06, 2005 at 07:53 UTC
    sort -n +0
Re: Sorting within a file
by tphyahoo (Vicar) on Mar 07, 2005 at 10:41 UTC
    Since noone's mentioned it, you could use tie::file to get the lines in from the file. (Not a sorting solution, but a nice way to grab from a file.) See

    http://www.perlmonks.org/index.pl?node_id=432116