Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Numerical sort with sort

Original Message
Name: ntropia
Date: April 19, 2006 at 04:33:04 Pacific
Subject: Numerical sort with sort
OS: Linux
CPU/Ram: Itanium 1Gb
Model/Manufacturer: Intel
Comment:
I'm trying to sort a file containing fields.
The two I use for sort contain numerical values, but they includes "+" or "-" signes.
Even using the -n flag of sort, final result is uncorrect, because it doesn't care about the signe.
There is a way to make that two-field sort, even not using the sort command? (I'm not expert of perl nor awk)
Thanks!



Report Offensive Message For Removal


Response Number 1
Name: lchi2000g
Date: April 19, 2006 at 07:28:15 Pacific
Subject: Numerical sort with sort
Reply: (edit)
Remove the "+" sign(s) in the input file

Luke Chi


Report Offensive Follow Up For Removal

Response Number 2
Name: ntropia
Date: April 19, 2006 at 08:37:59 Pacific
Subject: Numerical sort with sort
Reply: (edit)
Thanks! Now it works... but unfortunately it appears that just first field it's actually used for sorting.
The command line is:
sort -n -k 1.44,1.48 -k 2.36,2.41 tobeclustered.txt
to sort using respectively 6th and 5th fields. Some sample lines are like this:
1 1 | -8.05 | 98 | -4.45 | 25 |#########################
2 3 | -6.16 | 61 | -3.90 | 18 |##################
3 5 | -5.55 | 1 | -3.11 | 11 |###########
4 4 | -6.02 | 63 | -3.35 | 9 |#########
5 2 | -6.56 | 68 | -3.10 | 5 |#####
6 7 | -3.77 | 9 | -2.76 | 4 |####
7 17 | 0.14 | 32 | 1.14 | 3 |###
8 11 | -2.03 | 24 | -0.05 | 3 |###
9 8 | -2.42 | 99 | -0.16 | 2 |##
10 6 | -4.01 | 22 | -2.47 | 2 |##
11 19 | 0.99 | 81 | 2.18 | 2 |##
12 18 | 0.52 | 25 | 1.94 | 2 |##
13 13 | -0.95 | 64 | 0.62 | 2 |##
14 10 | -2.35 | 97 | -2.00 | 2 |##
15 9 | -2.35 | 5 | -2.35 | 1 |#
16 24 | 29.75 | 10 | 29.75 | 1 |#
17 23 | 8.80 | 62 | 8.80 | 1 |#
18 22 | 5.29 | 70 | 5.29 | 1 |#
19 21 | 1.97 | 73 | 1.97 | 1 |#
20 20 | 1.16 | 17 | 1.16 | 1 |#
21 16 | -0.17 | 38 | -0.17 | 1 |#
22 15 | -0.69 | 77 | -0.69 | 1 |#
23 14 | -0.75 | 19 | -0.75 | 1 |#
24 12 | -1.44 | 6 | -1.44 | 1 |#

My idea is that sort uses the 6th field to sort first, then, values showing the same value for that field will be sorted further using the 5th field. It's this correct?
Thanks,

Stefano


Report Offensive Follow Up For Removal

Response Number 3
Name: ntropia
Date: April 19, 2006 at 08:54:57 Pacific
Subject: Numerical sort with sort
Reply: (edit)
I was imprecise when I sayd "first field": I meant the first field used (the 6th, or the 1.44,1.48 as seen by sort).
I would like sort will order lines in a way that lines
...
7 17 | 0.14 | 32 | 1.14 | 3 |###
8 11 | -2.03 | 24 | -0.05 | 3 |###
...
will be ranked as:
8 11 | -2.03 | 24 | -0.05 | 3 |###
7 17 | 0.14 | 32 | 1.14 | 3 |###
accordingly first to the 6th field then to the 5th field.
I hope that's more clear, now... :-P

S.


Report Offensive Follow Up For Removal

Response Number 4
Name: lchi2000g
Date: April 19, 2006 at 09:58:12 Pacific
Subject: Numerical sort with sort
Reply: (edit)
input file:

1 1 | -8.05 | 98 | -4.45 | 25 |#########################
10 6 | -4.01 | 22 | -2.47 | 2 |##
11 19 | 0.99 | 81 | 2.18 | 2 |##
12 18 | 0.52 | 25 | 1.94 | 2 |##
13 13 | -0.95 | 64 | 0.62 | 2 |##
14 10 | -2.35 | 97 | -2.00 | 2 |##
15 9 | -2.35 | 5 | -2.35 | 1 |#
16 24 | 29.75 | 10 | 29.75 | 1 |#
17 23 | 8.80 | 62 | 8.80 | 1 |#
18 22 | 5.29 | 70 | 5.29 | 1 |#
19 21 | 1.97 | 73 | 1.97 | 1 |#
2 3 | -6.16 | 61 | -3.90 | 18 |##################
20 20 | 1.16 | 17 | 1.16 | 1 |#
21 16 | -0.17 | 38 | -0.17 | 1 |#
22 15 | -0.69 | 77 | -0.69 | 1 |#
23 14 | -0.75 | 19 | -0.75 | 1 |#
24 12 | -1.44 | 6 | -1.44 | 1 |#
3 5 | -5.55 | 1 | -3.11 | 11 |###########
4 4 | -6.02 | 63 | -3.35 | 9 |#########
5 2 | -6.56 | 68 | -3.10 | 5 |#####
6 7 | -3.77 | 9 | -2.76 | 4 |####
7 17 | 0.14 | 32 | 1.14 | 3 |###
8 11 | -2.03 | 24 | -0.05 | 3 |###
9 8 | -2.42 | 99 | -0.16 | 2 |##

command: (-t is the field delimiter)

sort -n -t" " -k 10,10 -k 8,8 input.txt

(For eample: in line of
15 9 | -2.35 | 5 | -2.35 | 1 |#
-2.35 is the 8th field
1 is the 10th field
)

output:

15 9 | -2.35 | 5 | -2.35 | 1 |#
24 12 | -1.44 | 6 | -1.44 | 1 |#
23 14 | -0.75 | 19 | -0.75 | 1 |#
22 15 | -0.69 | 77 | -0.69 | 1 |#
21 16 | -0.17 | 38 | -0.17 | 1 |#
20 20 | 1.16 | 17 | 1.16 | 1 |#
19 21 | 1.97 | 73 | 1.97 | 1 |#
18 22 | 5.29 | 70 | 5.29 | 1 |#
17 23 | 8.80 | 62 | 8.80 | 1 |#
16 24 | 29.75 | 10 | 29.75 | 1 |#
10 6 | -4.01 | 22 | -2.47 | 2 |##
14 10 | -2.35 | 97 | -2.00 | 2 |##
9 8 | -2.42 | 99 | -0.16 | 2 |##
13 13 | -0.95 | 64 | 0.62 | 2 |##
12 18 | 0.52 | 25 | 1.94 | 2 |##
11 19 | 0.99 | 81 | 2.18 | 2 |##
8 11 | -2.03 | 24 | -0.05 | 3 |###
7 17 | 0.14 | 32 | 1.14 | 3 |###
6 7 | -3.77 | 9 | -2.76 | 4 |####
5 2 | -6.56 | 68 | -3.10 | 5 |#####
4 4 | -6.02 | 63 | -3.35 | 9 |#########
3 5 | -5.55 | 1 | -3.11 | 11 |###########
2 3 | -6.16 | 61 | -3.90 | 18 |##################
1 1 | -8.05 | 98 | -4.45 | 25 |#########################

Luke Chi


Report Offensive Follow Up For Removal

Response Number 5
Name: ntropia
Date: April 20, 2006 at 12:17:43 Pacific
Subject: Numerical sort with sort
Reply: (edit)
Well, it's not exactly what I'm looking for, but probably it's my fault because it's a bit complicated for me too. To be honest, the first field was added by me, accordingly to a trick that (maybe) your self suggest in a prevoius post (a piping to a "| cat -n" after a first sort).
A simpler example, four fake lines:
INPUT:
X | XX | XX | XX | -4.9 | 4
X | XX | XX | XX | 1.9 | 4
X | XX | XX | XX | -8.9 | 10
X | XX | XX | XX | -3.9 | 20

There are two criteria I would use when sorting:
1. the 6th field, decreasing (higher values=better)
2. the 5th field, increasing (lower values=better)

That's the *real* problem.

It should be damn'easy, using the fields 1st and 6st:

FINAL OUTPUT (DESIRED)
X | XX | XX | XX | -3.9 | 20
X | XX | XX | XX | -8.9 | 10
X | XX | XX | XX | -4.9 | 4
X | XX | XX | XX | 1.9 | 4

...but how to manage the two sort orders (decreasing and increasing) in the same time?

Anyway, my best thanks for your patience up to now.

Stefano


Report Offensive Follow Up For Removal


Response Number 6
Name: lchi2000g
Date: April 21, 2006 at 15:11:24 Pacific
Subject: Numerical sort with sort
Reply: (edit)
Once -r, decending, in sort comand is specified, ALL columns in the input file will be sorted in decending order.

If -r is not specified, ALL coulmns in the input file will be sorted in ascending order.

sort command in Unix is not smart enough.

Luke Chi


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Numerical sort with sort

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC