Why is sort not working properly here ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why is sort not working properly here ?
# 1  
Old 03-11-2013
Why is sort not working properly here ?

Platform: RHEL 5.4
In the below text file I have strings like following.

Code:
$ cat /tmp/mytextfile.txt
DISK1
DISK10
DISK101
DISK102
DISK103
DISK104
DISK105
DISK106
DISK107
DISK108
DISK109
DISK110
DISK111
DISK112
DISK113
DISK114
DISK115
.
.
.
<output snipped for better readability>
.
.
DISK459
DISK46
DISK460
DISK461
DISK462
DISK47
DISK48
DISK49
DISK5
DISK50
DISK51
DISK52
DISK53
DISK54
DISK55
DISK56
DISK57
DISK58
DISK6
DISK7
DISK8
DISK9

I wanted to sort the output based on the number after the 'DISK' string. My expected output :
Code:
 
DISK1
DISK2
DISK3
DISK4
DISK5

But when I use sort, the output looks like below


Code:
 
$ sort /tmp/mytextfile.txt
DISK1
DISK10
DISK101
DISK102
DISK103
DISK104
DISK105
DISK106
DISK107
DISK108
DISK109
DISK110
.
.
.
<output snipped for better readability>

# Trying with -n option. But , still the same output as above.
Code:
 
$ sort -n /tmp/mytextfile.txt
DISK1
DISK10
DISK101
DISK102
DISK103
DISK104
DISK105
DISK106
DISK107
DISK108
DISK109
DISK110

How can I get the expected output using sort or any other utility ?
# 2  
Old 03-11-2013
Use:
Code:
# sort -n -k 1.5 /tmp/mytextfile.txt

This User Gave Thanks to Klashxx For This Post:
# 3  
Old 03-11-2013
Thank you. It works !! But how does this work?
What is 1.5 ?

This is what sort's man page says about -k
Code:
 
-k, --key=POS1[,POS2]
              start a key at POS1, end it at POS2 (origin 1)

Couldn't understand how -k option works from the man page's explanation.

---------- Post updated at 10:50 AM ---------- Previous update was at 08:56 AM ----------

anyone ?
# 4  
Old 03-11-2013
Do not bump up posts.

If someone doesn't answer your post immediately, wait! We are not on call.

-k tells it what column and what characters of each column to sort on. There's more detail about what denotes a column below since it's actually a bit complicated.

What does most of the work here, I think, is -n, "numeric sort".
# 5  
Old 03-11-2013
See the man page, roughly 35 lines down from your citation:
Quote:
POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b
is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter
ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.
# 6  
Old 03-11-2013
Since DISK is a constant four, you want to key your sort on the first field character 5 and following, numeric ordering. In the the ol day, this was '-n +0.4 -0.99' but now it is -k1.5,1.99n, as field and character offsets in +- ar zero based and in -k, one based.

If you rename your volumes with leading zeros then there is no problem with simple sort.
This User Gave Thanks to DGPickett For This Post:
# 7  
Old 03-11-2013
Yes, you save a lot of trouble if just rename with leading zeroes. Of course, might start with DISK001 and oops now we need a DISK1000, so need to put in enough capacity.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I sort data in column properly?

Can i use 'column' command to get the required 3rd column output? Input example: 1 2 345678 90 2 2 356 42 3 3 8265 55 Output required: 1 2 345678 90 2 2 356 42 3 3 8265 55 Basically i want the 3rd column to be justified to the right, instead of left.... (3 Replies)
Discussion started by: nurul_nadzirah
3 Replies

2. Shell Programming and Scripting

Expansion not working properly

I'm using an Ubuntu machine and expansion is not working properly. What would cause this? Do I need to check for any particular bash packages? $ ipcs -m | grep $USER | awk '{printf "%s ",$2}' $ ipcs -m | grep UNF | awk '{printf "%s ",$2}' 294912 1048577 425986 688131 786436 1245189... (14 Replies)
Discussion started by: cokedude
14 Replies

3. Linux

rexec not working properly

Hi, I am trying to enable rexec to automate certain tasks(it has to be rexec, not ssh or any other due to the system environment), so after switching to linux, I followed the certain instructions that were laid out in the web. My operating system is fedora 17, so I first installed the... (1 Reply)
Discussion started by: wringer
1 Replies

4. Red Hat

sudo is not working properly

This is the first time for using sudo for me. # visudo ## Allows people in group admin to run all commands %admin ALL=(ALL) ALL # groupadd admin # useradd temp # usermod -a -G admin temp # id temp uid=506(temp) gid=506(temp) groups=506(temp),507(admin) # #sudo... (5 Replies)
Discussion started by: getrue
5 Replies

5. Shell Programming and Scripting

Script not working..."sort" not working properly....

Hello all, I have a file - 12.txt cat 12.txt =============================================== Number of executions = 2 Total execution time (sec.ms) = 0.009883 Number of executions = 8 Total execution time (sec.ms) = 0.001270 Number of... (23 Replies)
Discussion started by: Rahulpict
23 Replies

6. UNIX for Advanced & Expert Users

Sendmail is not working properly

Hi All, Can any one help me to solve the issue. The Issue is, i have started the sendmail service on my RHEL 4 update 6 box, I am able to send the mail from my box to almost all of the Email Id's except few. Exampe, test mail. . Output is :the message is sent. now if I send the... (2 Replies)
Discussion started by: akhtar.bhat
2 Replies

7. Shell Programming and Scripting

\n not working properly

Hi all, I'm trying to generate a series of txt files starting from a plain csv file part of my code: #!/bin/ksh INSTALLDIR=/Users/ME/Installdir CSV=CSV.csv TMP=/tmp/$(basename $0).txt tr -s "\r" "\n" < /$INSTALLDIR/$CSV > $TMP function Makefiles { printf '%24s:%30s\n' "sometext"... (1 Reply)
Discussion started by: Jive Spector
1 Replies

8. Shell Programming and Scripting

Korn: How to zero fill df output so it will sort properly

I'm looking for a way in Korn shell to zero fill (or space fill) the output from df so that it will sort properly. "Raw" output from df -k: df -k Filesystem kbytes used avail capacity Mounted on /dev/vx/dsk/rootvol 4131866 3593302 497246 88% / /proc ... (9 Replies)
Discussion started by: shew01
9 Replies

9. Programming

y is this not working properly?

#include <stdio.h> #include <sys/types.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> struct stat s; main() { char c; if (fork()==0) { system("clear"); do { printf("myAI\\>§ "); scanf("%s",c); if(stat(c,&s)>-1) {... (3 Replies)
Discussion started by: C|[anti-trust]
3 Replies

10. UNIX for Dummies Questions & Answers

Keyboard not working properly...

Hello Again, Those that have noticed my earlier posts will know that I have succesfully installed Solaris 8 onto my pc. I haven't been able to get x-server working (i think it doesn't like my video card) though I've been able to log into root (with a bit of help from unix forums :o ) and have... (2 Replies)
Discussion started by: timresh
2 Replies
Login or Register to Ask a Question