Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-30-2005, 11:31 AM   #1
LQ Newbie
Registered: Sep 2004
Posts: 11

Rep: Reputation: 0
Unix data sort question

Using the shell's sort command, I can sort a data file, even specificy what columns to sort, and if the data should be sorted as alphanumeric, or numeric.

My problem is when I want to sort a file on multiple columns, where some of those columns needs to be sorted as alpha, and other columns as numeric.

Sort doesn't seem to handle this... it can only treat data as alpha or numeric, AFAIK.

How, using native shell tools (can expand into sed, awk, if necessary) can I get my data sorted as I want?

As an example I would like:

Z,hello world1,101
AZ,hello world1,100
AZ,hello world1,102
AA,hello world1,50
AA,hello world1,6
AZ,hello world1,30

to be sorted as

AA,hello world1,6
AA,hello world1,50
AZ,hello world1,30
AZ,hello world1,100
AZ,hello world1,102
Z,hello world1,101

TODAY, using sort

ALPHA (default) example
sort -t, -k 1,1 -k 3,3 filename > results in:

AA,hello world1,50
AA,hello world1,6
AZ,hello world1,100
AZ,hello world1,102
AZ,hello world1,30
Z,hello world1,101

Numeric (-n) example
sort -n -t, -k 1,1 -k 3,3 filename > results in:

AA,hello world1,6
AZ,hello world1,30
AA,hello world1,50
AZ,hello world1,100
Z,hello world1,101
AZ,hello world1,102

Last edited by amytys; 06-30-2005 at 11:34 AM.
Old 06-30-2005, 12:43 PM   #2
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
sort -t, -k1,1d -k3,3n filename
Old 07-01-2005, 06:24 AM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: Slack, Debian, Mint, Puppy, Raspbian
Posts: 3,474

Rep: Reputation: 222Reputation: 222Reputation: 222
read the 'man' page ;-)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Redhat Data Sort eivets Linux - Software 3 03-22-2005 03:52 PM
gawk & sort commands in unix fanatic_ravi Linux - Software 0 01-25-2005 05:10 AM
Free data wipe tools for Unix davholla Linux - General 1 12-21-2004 12:35 PM
how to send data from winsock to unix sock husniteja Programming 6 08-25-2004 07:40 AM
unix sort from the right side dazdaz Linux - General 6 11-11-2003 09:34 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration