Visit Our UNIX and Linux User Community


Sorting - Reg.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting - Reg.
# 1  
Old 04-23-2010
Sorting - Reg.

Hi masters,
I have one doubt,
lets's say file1 has the following contents,
Code:
1
2.0
3.1
5.5
7
5.10
5.9

How to sort these contents to get the o/p like
Code:
1
2.0
3.1
5.5
5.9
5.10
7

Note:5.10 should come after 5.9.
Please provide your comments.
Thank you

Last edited by vgersh99; 04-23-2010 at 10:19 AM.. Reason: code tags, please!
# 2  
Old 04-23-2010
Code:
sort -t '.' -k1,1 -k2,2 -n myFile

# 3  
Old 04-23-2010
I get an error with the code of vgersh99, try:
Code:
sort -t '.' -k 1,1n -k 2,2n file

# 4  
Old 04-23-2010
Quote:
Originally Posted by Franklin52
I get an error with the code of vgersh99, try:
Code:
sort -t '.' -k 1,1n -k 2,2n file

strange - works fine under Solaris9.

you can try - not sure the order matters:
Code:
sort -t '.' -n -k1,1 -k2,2 myFile


Last edited by vgersh99; 04-23-2010 at 12:28 PM..
# 5  
Old 04-23-2010
A quick observation - neither of these solutions work for non-trivial floating point numbers, because the order of the most significant digit is the reverse on the right of the decimal point as compared to the left.

So, while 1034 < 10034 is true; 0.1034 < 0.10034 isn't.

An example is below:

Code:
$
$
$ cat f0
1
2.0
2.01
3.1
5.10034
5.5
7
5.10
5.1034
5.9
$
$ sort -t"." -k1,1n -k2,2n f0
1
2.0
2.01
3.1
5.5
5.9
5.10
5.1034
5.10034
7
$
$

Note that the floating point numbers in red are not in ascending order.

I am sure will be something for this in the shell itself, but here's a simple Perl solution:

Code:
$
$ cat f0
1
2.0
2.01
3.1
5.10034
5.5
7
5.10
5.1034
5.9
$
$ perl -lne 'chomp; push @x, $_; END {print foreach sort @x}' f0
1
2.0
2.01
3.1
5.10
5.10034
5.1034
5.5
5.9
7
$
$

tyler_durden
# 6  
Old 04-23-2010
Code:
$  cat file
1
2.0
2.01
3.1
5.10034
5.5
7
5.10
5.1034
5.9
$ sort -kn1 file
1
2.0
2.01
3.1
5.10
5.10034
5.1034
5.5
5.9
7

# 7  
Old 04-23-2010
Quote:
Originally Posted by vgersh99
strange - works fine under Solaris9.

you can try - not sure the order matters:
Code:
sort -t '.' -n -k1,1 -k2,2 myFile

I got the error with a HP-UX on my work but it works fine on my Debian system.

anbu23 and durden_tyler, the second column must be also sorted in numeric order so the output should be like:
Code:
3.1
5.5
5.9
5.10
5.1034
5.10034
7


Previous Thread | Next Thread
Test Your Knowledge in Computers #106
Difficulty: Easy
A kernel is at the heart of the Linux OS; and this kernel acts as an interface between the user and underlying hardware.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

REG Expression

Need your help in creating regular expression for particular set. let say I have given two dates 20130623 to 20140625. I need to create regular for the dates which fall in between above two dates. (4 Replies)
Discussion started by: gvkumar25
4 Replies

2. Shell Programming and Scripting

Reg expressions

Hi, I would like to grep for a string within a tag, can someone provide some assistance in how to do it? So I would like to use the grep command to find a string like: <tag>sometext<tag> because the sometext can be any number of characters or an type of number or lettering, what expression... (1 Reply)
Discussion started by: cyberfrog
1 Replies

3. AIX

reg DS_LVZ

HI in a vg i want to display the lv name & whether the LV is enabled with DS_LVZ parameter? I used #lsvg -o | grep vgname | lsvg -il this gives the output of lv's in the vg. buti treid with lsvg -o | grep vgname| lsvg -il | egrep "LOGICAL VOLUME|DS_LVZ" but No... (5 Replies)
Discussion started by: balumurugesh
5 Replies

4. Solaris

Reg. VXVM

Hi Guys, I have a doubt either to Reboot the server after Replacing the disk0. I have two disks under vxvm root mirrored and i had a problem with primary disk so i replace the disk0 failed primary disk and then mirrored. After mirroring is it reboot required ? (7 Replies)
Discussion started by: kurva
7 Replies

5. Solaris

Reg: ZONES

HI Friends, What is the min. Requirement to install Solaris ZONEs, i am using INTEL PC at home and i allready installed Solaris 10 can i configure ZONES in it, and i want to know the basic information of ZONES. Thanks in Advance. (3 Replies)
Discussion started by: kurva
3 Replies

6. UNIX for Dummies Questions & Answers

Reg: MAILX

Hi all, I am trying to send a mail by using MAILX option to my YAHOO-Id. It is giving the following error. Can any one help me to find what is the problem? Do i need to get any kind of settings in my UNIX box for using MAILX? The bounce mail is as below: Message 1: From MAILER-DAEMON Tue... (2 Replies)
Discussion started by: Raamc
2 Replies

7. Shell Programming and Scripting

Reg: Gzip

Hi , I want gzip a folder te55 which has got 3 files test1.test2,test3 the name of the gzipped folder should be te55.gz with the 3 files as test1,test2,test3 itself... Is it possible... thanks in advance sam (5 Replies)
Discussion started by: sam99
5 Replies

8. UNIX for Dummies Questions & Answers

Help with Reg. Expression

I need help with this: Can any one tell me what does these below mean: 1. "\(.\).*") != '/' 2. sed 's+^\./++;s+/.*++' 3. sed "s+${f}/+ + Thanks in advance (7 Replies)
Discussion started by: moe2266
7 Replies

9. UNIX for Advanced & Expert Users

Reg DB values

Hi, I would like to call my Data Base procedure through unix, my procedure returns 4 values i.e. (4 out variables), how to assign 4 out variables to unix variables. Ex: xxat_safety_stock_main_p( retcode => l_retcode ,errbuf => l_errbuf... (2 Replies)
Discussion started by: mpkirankumar
2 Replies

10. SCO

reg:rm -R is not working

I am installing the my package in SCO unix 5.0 .so it will create some temporary directories and files and same will delete.So , 'rm -R ' is not working in system .in the package only it has given this command,so now without this command working we can not go forward. so kindly help in this... (1 Reply)
Discussion started by: mokri_1980
1 Replies

Featured Tech Videos