Sort command usage


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort command usage
# 1  
Old 09-30-2013
Sort command usage

I have one file like this:
Code:
NEW
/ifs/SQL_Backups3/SQL_SharePoint1
NEW
/ifs/SQL_Backups/SQL_SharePoint

This can be easily sorted by the following command:
Code:
cat  file| sort -k3,3n

But I have another file like this:

Code:
/Pool0/local/Benchmark
/Pool0/local/CRAD
/Pool0/local/crdhw/espresso_scratch1
/Pool0/local/crdhw/neo_scratch1
/Pool0/local/crdhw/neo_scratch10
/Pool0/local/crdhw/neo_scratch2
/Pool0/local/crdhw/neo_scratch36
/Pool0/local/crdhw/neo_scratch4
/Pool0/local/crdhw/neo_scratch7

This file can't be sorted by file |sort -k5,5n or any other command.
The only way I've gotten it to sort is by the following:

Code:
file |sort -t/ -k5.12n

I am wondering if there is a certain criteria that will cause you to need to use a character numerical sort instead of a column sort? I am sort of confused on this.

In addition, I am also wondering if sort allows you to use the case insensitive option with numerical sorts. What if I had Scratch4 and scratch4 as paths? From what I can see, you can't use the numerical sort and the -f option even if you specify the column and character to use. It will always put the upper case at the top. Is there a way to turn this off through collation or LC_COLLATE?

Last edited by newbie2010; 09-30-2013 at 03:57 PM..
# 2  
Old 09-30-2013
Quote:
Originally Posted by newbie2010
I have one file like this:
Code:
NEW
/ifs/SQL_Backups3/SQL_SharePoint1
NEW
/ifs/SQL_Backups/SQL_SharePoint

This can be easily sorted by the following command:
Code:
cat  file| sort -k3,3n

Since you didn't give sort a -t option, with the data you have above, the command:
Code:
cat  file| sort -k3,3n

is an inefficient equivalent of the command:
Code:
sort file

(There is no third field. And even if you used -t/, the start of the 3rd field is not numeric.)
Quote:
Originally Posted by newbie2010
But I have another file like this:

Code:
/Pool0/local/Benchmark
/Pool0/local/CRAD
/Pool0/local/crdhw/espresso_scratch1
/Pool0/local/crdhw/neo_scratch1
/Pool0/local/crdhw/neo_scratch10
/Pool0/local/crdhw/neo_scratch2
/Pool0/local/crdhw/neo_scratch36
/Pool0/local/crdhw/neo_scratch4
/Pool0/local/crdhw/neo_scratch7

This file can't be sorted by file |sort -k5,5n or any other command.
The only way I've gotten it to sort is by the following:

Code:
file |sort -t/ -k5.12n

I am wondering if there is a certain criteria that will cause you to need to use a character numerical sort instead of a column sort? I am sort of confused on this.
This is simple. If you have a string of digits that are not all the same length (same number of digits) and you want to sort them by numeric value (e.g., 5, 25, 100) instead of as a string by collating sequence (i.e., 100, 25, 5), then you need to sort that portion of your input file using a numeric sort instead of using the default alphanumeric sort.
Quote:
Originally Posted by newbie2010
In addition, I am also wondering if sort allows you to use the case insensitive option with numerical sorts. What if I had Scratch4 and scratch4 as paths? From what I can see, you can't use the numerical sort and the -f option even if you specify the column and character to use. It will always put the upper case at the top. Is there a way to turn this off through collation or LC_COLLATE?
You didn't have any data that would show whether or not a case insensitive sort would matter. "B" comes before "CRA" which comes before "crd" with both case sensitive and case insensitive sorts.

Here is a script that shows a couple of slightly more complex input files, the commands I used to sort them, and the sorted output that seems to match what you're trying to do:
Code:
echo 'file1 contents:'
cat file1
printf '\nsorted file1 contents:\n'
sort -f -t/ -k1,3.11 -k3.12,3n -k4.1,4.14 -k4.15,4n file1
printf '\nfile2 contents:\n'
cat file2
printf '\nsorted file2 contents:\n'
sort -f -t/ -k1,5.11 -k5.12,5n -k5.12,5.16 -k5.17,5n file2

The output produced (with my expanded test files) is:
Code:
file1 contents:
NEW
/IFS/SQL_Backups3/SQL_SharePoint2
/Ifs/SQL_Backups2/SQL_SharePoint7
/iFs/SQl_Backups2/SQL_SharePoint10
/ifS/SQL_Backups3/SQL_SharePoint112
NEW
/ifs/SQL_Backups/SQL_SharePoint

sorted file1 contents:
/ifs/SQL_Backups/SQL_SharePoint
/Ifs/SQL_Backups2/SQL_SharePoint7
/iFs/SQl_Backups2/SQL_SharePoint10
/IFS/SQL_Backups3/SQL_SharePoint2
/ifS/SQL_Backups3/SQL_SharePoint112
NEW
NEW

file2 contents:
/Pool0/local/Benchmark
/Pool0/local/CRAD
/Pool0/local/FRANK
/Pool0/local/crdhw/espresso_scratch5
/Pool0/local/crdhw/espresso_scratch10
/Pool0/local/crdhw/espresso_scratch1
/Pool0/local/crdhw/neo_scratch1
/Pool0/local/crdhw/neo_scratch10
/Pool0/local/crdhw/neo_scratch2
/Pool0/local/crdhw/neo_scratch36
/Pool0/local/crdhw/neo_scratch4
/Pool0/local/crdhw/neo_scratch7

sorted file2 contents:
/Pool0/local/Benchmark
/Pool0/local/CRAD
/Pool0/local/crdhw/espresso_scratch1
/Pool0/local/crdhw/espresso_scratch5
/Pool0/local/crdhw/espresso_scratch10
/Pool0/local/crdhw/neo_scratch1
/Pool0/local/crdhw/neo_scratch2
/Pool0/local/crdhw/neo_scratch4
/Pool0/local/crdhw/neo_scratch7
/Pool0/local/crdhw/neo_scratch10
/Pool0/local/crdhw/neo_scratch36
/Pool0/local/FRANK

Is this what you were trying to do? Please take another look at the sort man page to figure out why this works. If you still have questions about how the sort keys work in these two sort commands, let me know what doesn't make sense.
# 3  
Old 09-30-2013
Case insensitive sort

Yes I see how it works now. I actually was using a file I copied from somewhere else and the -k3,3n only appeared to work. I was able to get the file to sort through cat file | sort -t/ -k4.15

Code:
/ifs/SQL_Backups3/SQL_SharePoint
/ifs/SQL_Backups/SQL_SharePoint1

The other question which you even mentioned is in regard to case sensitivity. Now it seems to be impossible to sort both numerically AND also disregard case, even with the f option. I've tried using different sort and uniq options and if I have a number and a word, it doesn't work. For example:
Code:
NEW
/ifs/DumpsterArchive
NEW
/ifs/data
NEW
/ifs/eas_archive
NEW
/ifs/data13
NEW
/ifs/data8
NEW
/ifs/data5
NEW
/ifs/data6
NEW
/ifs/data7

Code:
file |sort -t/ -k3.5n

works just great but even with
Code:
sort -f -t/ -k3.5n

it still puts Dumpster at the very top of the file.
I therefore tried with
Code:
 tr [a-z] [A-Z]

but as this would make all the letters upper case then it wouldn't work in the end.
So I tried sed patterns but unless the first letter was the cause of the case sensitive sort every time, I am not sure it would work.

Does sort allow the -f with the -n so that you can sort alphanumerically? If not I assume I could use some form of sed to save the pattern

Last edited by Don Cragun; 09-30-2013 at 08:20 PM.. Reason: Add missing CODE tags.
# 4  
Old 09-30-2013
That's funny.
Code:
sort -f -t/ -k1,3.4 -k3.5,3n file

produces:
Code:
/ifs/data
/ifs/data5
/ifs/data6
/ifs/data7
/ifs/data8
/ifs/data13
/ifs/DumpsterArchive
/ifs/eas_archive
NEW
NEW
NEW
NEW
NEW
NEW
NEW
NEW

which looks right to me.

PS. Please use:
Code:
sort options file

rather than the inefficient and wasteful:
Code:
cat file | sort options

or the syntactically incorrect:
Code:
file | sort options

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-01-2013
Thanks!

I see now. Also
Code:
sort -f -t/ -k1,3.4 -k3.5n

would mean the following:

sort case insensitive on the 1st to the third column from character 4 and then sort numerically using the / as a delimiter and then the third column , fifth character?
Seems close. I will try to review this by checking out the other sort usages you so kindly supplied me with.
# 6  
Old 10-01-2013
Quote:
Originally Posted by newbie2010
I see now. Also
Code:
sort -f -t/ -k1,3.4 -k3.5n

would mean the following:

sort case insensitive on the 1st to the third column from character 4 and then sort numerically using the / as a delimiter and then the third column , fifth character?
The options supplied to sort are applied to the entire line of input and not just parts of the line with the exception of the "-k" option. So the above sort should be read as...
Code:
sort the entire file case insensitive due to "-f" while breaking up the entire line into fields as per the forward slash delimiter "-t/"
and first do an ascii sort on fields (start: 1st field; end:  4th character of 3rd field) and finally sort the rest of the line numerically.

Quote:
Originally Posted by newbie2010
Seems close. I will try to review this by checking out the other sort usages you so kindly supplied me with.
man sort for details and hope this helps...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Usage of '.' in MV command

Hi, Could you please let me know, why we should not use '.' in move command, if we use it, is it something wrong.. Please share the details on it. /home/rahualux/emp.csv /home/rahualux/details/employee_files/. Or other example for mutlipile files /home/rahualux/*.csv... (3 Replies)
Discussion started by: rahualux
3 Replies

2. Shell Programming and Scripting

Help to sort out... Possible use of sort command

I have an input like 4.3.6.66 4.3.6.67 4.3.6.70 4.3.6.25 4.3.6.15 4.3.6.54 4.3.6.44 4.3.6.34 4.3.6.24 4.3.6.14 4.3.6.53 4.3.6.43 4.3.6.49 4.3.6.33 4.3.6.52 4.3.6.19 4.3.6.58 4.3.6.42 (5 Replies)
Discussion started by: dnam9917
5 Replies

3. Shell Programming and Scripting

cp -v command usage?

I am trying to output a log file from cp usage. I think this can be achieved. In my code I have this. cp -i -v ~/files/* ~/backups/oldfiles/;; > ~/logs/logfile.logThe error I get is "syntax error near unexpected token '>' What am I missing? (7 Replies)
Discussion started by: gameinn
7 Replies

4. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

5. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

6. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

7. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

8. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

9. UNIX for Advanced & Expert Users

find command usage

I usually ise find to search a file or name on the unix, since I am not administrator, there will be many line appear 'cannot access',usually a hundred of lines. How can I prevent this line coming out? only show I want? The command I use is : find / -name abcdef -print Thank all expert. (1 Reply)
Discussion started by: zp523444
1 Replies
Login or Register to Ask a Question