LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 06-30-2005, 10:31 AM   #1
amytys
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 >filename.srt 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 >filename.srt 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 10:34 AM.
 
Old 06-30-2005, 11:43 AM   #2
zeropash
Member
 
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, 05:24 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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


Reply



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 02:52 PM
gawk & sort commands in unix fanatic_ravi Linux - Software 0 01-25-2005 04:10 AM
Free data wipe tools for Unix davholla Linux - General 1 12-21-2004 11:35 AM
how to send data from winsock to unix sock husniteja Programming 6 08-25-2004 06:40 AM
unix sort from the right side dazdaz Linux - General 6 11-11-2003 08:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:13 PM.

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