[CLUE-Tech] sort puzzler

Jed S. Baer thag at frii.com
Wed Oct 22 20:36:14 MDT 2003


On Wed, 22 Oct 2003 20:00:50 -0600
Jeff Cann <j.cann at isuma.org> wrote:

> Greetings.
> 
> A colleague asked me about whether this problem can be solved with UNIX
> sort.  I'm could not think of a way from the shell to solve this
> problem.  I told her I would ask my 'guru' list on clue-tech.

...

> I need to sort a file with columns 1-3 sorted ascending, column 4
> descending. I cannot get any combinations of the unix sort command to
> output this, so I think I need to move to another approach such as perl
> or awk. 
> 
> I can sort on key 4 to get that in descending order with sort  -rn -t \|
> -k 4 
> 
> But I cannot work the combination of sorts... 
> 
> $ sort -t \| +1 -3 test.ibm 
> IBSALQAVC|006066|01|-1|20065|20065 

Well, first off, it appears she wants to sort on _fields_ not columns.

Looking at the sort info page, I find the following example:

   * Sort numerically on the second field and resolve ties by sorting
     alphabetically on the third and fourth characters of field five.
     Use `:' as the field delimiter.

          sort -t : -k 2,2n -k 5.3,5.4

So, it appears that she would use:

  $ sort -t \| -k 1,3 -k4r

although whether that specification would work on any Unix sort
implementation, I can't say.

If that doesn't, for some reason work, there's always the option of just
importing the file into a database, and dumping it back out using an
"order by" query.

I note also that her usage of an obsolete posix syntax doesn't match what
she described. As I understand it, to do what she says, using the old
syntax, she'd type:

  $ sort -t \| +0 +1 +2 -3

or somthing like that. IOW, I think she mis-specified her keys, regardless
of the old/new syntax issue.

jed
-- 
... it is poor civic hygiene to install technologies that could someday
facilitate a police state. -- Bruce Schneier



More information about the clue-tech mailing list