Try following column-oriented sort script. Columns are expected to be delimited by one or more whitespaces. You can specify column(s) and column type to be used for sorting.

#!/usr/bin/perl -w # # Description: # Column oriented sort utility # my @columns; my $column; my $type; my $first; my $line; my $sort_code; my $sort_tmpl = q|sub sort_sub { my @col_a = split(/\s+/, $a); my @col_b = split(/\s+/, $b); __SORT_CODE__ }|; sub print_usage { my $exit = shift || 0; my $error = shift; print qq|Usage: $0 <column>[,<column>]+ [file] Column oriented sort. Input data columns are expected to be delimited by one or more whitespaces. Param(s): column - number of column (starting from 1) to be used for sorting. Use suffix to specify column type. Available suffixes a - column is text (default) d - column is decimal number x - column is hexadecimal number file - source data file. Data are read from STDIN if omitted Example: $0 3d,2x,4 /input/data Sort content of /input/data using 1. 3rd column as decimal number, 2. 2nd column as hexadecimal number and 3. finaly 4th column as text |; print "ERROR: $error\n" if ($error); exit $exit; } print_usage(1, "Missing params") if (@ARGV < 1); print_usage(0) if ($ARGV[0] =~ /^--?h(e(lp?)?)?/); @columns = split(/,/, shift @ARGV); print_usage(1, "Incorrect 'column(s)' param") if (!@columns); $first = 1; $line = <>; for (@columns) { $sort_code .= "|| " if (!$first); if ($_ =~ /^(\d+)(.)?/) { $column = $1 - 1; $type = $2 ? lc($2) : 'a'; $type = 'a' if ($type !~ /^(?:a|d|x)$/); if ($type eq 'x') { $sort_code .= "hex(\$col_a[$column]) <=> hex(\ +$col_b[$column]) "; } elsif($type eq 'd') { $sort_code .= "\$col_a[$column] <=> \$col_b[$c +olumn] "; } else { $sort_code .= "\$col_a[$column] cmp \$col_b[$c +olumn] "; } } else { print_usage(1, "Incorrect 'column(s)' param"); } $first = 0; } $sort_code .= ";\n"; $sort_tmpl =~ s/__SORT_CODE__/$sort_code/; eval $sort_tmpl; print sort sort_sub $line, <>;

In reply to Re: Sorting by column by ph0enix
in thread Sorting by column by Sun751

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.