Difference of Sort -n -k2 -k3 & Sort -n -k2,3


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Difference of Sort -n -k2 -k3 & Sort -n -k2,3
# 1  
Old 01-19-2017
Difference of Sort -n -k2 -k3 & Sort -n -k2,3

Hi,

Could anyone kindly show me a link or explain the difference between

Code:
sort -n -k2 -k3 & sort -n -k2,3

Also, if I like to remove the row with repetition at both $2 and $3, Can I safely use

Code:
sort -u -k2 -k3

Example;
Code:
100 20 30
100 20 30

So, both $2 and $3 are same and I would like to have

Code:
100 20 30

Thanks,

Last edited by rbatte1; 01-20-2017 at 07:42 AM.. Reason: Spelling
# 2  
Old 01-19-2017
According to the standards, the command:
Code:
sort -n -k2 -k3 file

sorts lines in file into increasing order evaluating field 2 and all fields following it as numeric values and if fields 2 to the end of the line compare equal to another line it then compares field 3 and all fields following it as numeric values and if two lines still compare as equal it then compares the entire line as a string of bytes to determine which line will come first.
Code:
sort -n -k2,3 file

sorts lines in file into increasing order evaluating the 2nd and 3rd fields as numeric values and if the numeric values of those two fields are the same when comparing two lines the entire line will be compared as a string of bytes to determine which line will come first.

If what you are trying to do is sort numeric values in fields 2 and 3 in file and only keep one line in cases when there are multiple lines with identical numeric values in those two fields, you would want to use:
Code:
sort -u -n -k2,3 file

if field 2 is your primary sort key and field 3 is your secondary key or:
Code:
sort -u -n -k3,3 -k2,2 file

if field 3 is your primary sort key and field 2 is your secondary key.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-20-2017
Quote:
Originally Posted by Don Cragun
[..]
Code:
sort -n -k2,3 file

sorts lines in file into increasing order evaluating the 2nd and 3rd fields as numeric values and if the numeric values of those two fields are the same when comparing two lines the entire line will be compared as a string of bytes to determine which line will come first.
[..]
I believe this would combine from the second to the third field as one sort key.

To achieve evaluating the 2nd and 3rd fields as separate numeric values, I think one would need:
Code:
sort -n -k2,2 -k3,3 file


Compare for example:
Code:
$ echo "1   2
2   3
2  2
2 1" | sort -n -k1,2
1   2
2   3
2  2
2 1

to
Code:
$ echo "1   2
2   3
2  2
2 1" | sort -n -k1,1 -k2,2
1   2
2 1
2  2
2   3

These 3 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort & Uniq -u

Hi All, Below the actual file which i like to sort and Uniq -u /opt/oracle/work/Antony/Shell_Script> cat emp.1st 2233|a.k. shukula |g.m. |sales |12/12/52 |6000 1006|chanchal singhvi |director |sales |03/09/38 |6700... (8 Replies)
Discussion started by: Antony Ankrose
8 Replies

2. Shell Programming and Scripting

UNIX compare, sort lines and append difference

To make it easier, i gave following example. It is not homework or classwork. Instead, i have a huge csv file dump from tsql with 15 columns and around 300 rows. I was able to extract content that needs to be really converted. Here is the extract: ES FP,B1ES FP,70000,I,SL22,SL22 (70000) ES... (8 Replies)
Discussion started by: nike27
8 Replies

3. Shell Programming and Scripting

UNIX compare, sort lines and append difference

To make it easier, i gave following example. It is not homework or classwork. Instead, i have a huge csv file dump from tsql with 15 columns and around 300 rows. I was able to extract content that needs to be really converted. Here is the extract: ES FP,B1ES FP,70000,I,SL22,SL22 (70000) ES... (0 Replies)
Discussion started by: nike27
0 Replies

4. Shell Programming and Scripting

UNIX compare, sort lines and append difference

Hi, I have a file that needs to be converted: content is: a, b, 4 a ,b, 5 x, y, 1 a, b, 1 x, y, 3 how can i get: a, b, 1|4|5 x,y 1|3 (1 Reply)
Discussion started by: nike27
1 Replies

5. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

6. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

7. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

8. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

9. Shell Programming and Scripting

difference in unix vs. linux sort

Hi, I am using some codes that have been ported from unix to linux, and now the sorting no longer results in the desired ordering. I'm hoping to find a way to mimic the unix sort command in linux. The input file is structured the following: $> cat file.txt... (6 Replies)
Discussion started by: aj.schaeffer
6 Replies

10. 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
Login or Register to Ask a Question