Trying to sort words and numbers associated with them.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Trying to sort words and numbers associated with them.
# 1  
Old 03-22-2011
Trying to sort words and numbers associated with them.

Hi. I have a file containing words and numbers associated with them as follows -
Code:
c 2
b 5
c 5
b 6
a 10
b 16
c 18
a 19
b 21
c 27
a 28
b 33
a 76
a 115
c 199
c 251
a 567
a 1909

I want to sort them so that I get something like
Code:
a 10
a 19
a 28
a 76
a 115
a 567
a 1909
b 5
b 6
b 16
b 21
b 33
c 2
c 5
c 18
c 27
c 199
c 251

So you observe that each word is listed in the alphabetical order. At the same time, the numbers for each word also follow in the numerical order. I tried using Unix sort, but unix sort gives a problem for entries like
Code:
a 10
a 115
a 19

Here, the "correct" order should be
Code:
a 10
a 19
a 115

How is it possible to achieve this? Someone please help me.

Last edited by Franklin52; 03-23-2011 at 04:56 AM.. Reason: Please use code tags
# 2  
Old 03-22-2011
Unix/Linux sort can handle fields. Older use +FIELD<type> -FIELD... so
Code:
sort +0 -1 +1n -2

newer sort support the -k option and field positions start at 1 instead of 0.
Code:
sort -k1,1 -k2,2n


Last edited by Franklin52; 03-23-2011 at 04:56 AM.. Reason: Please use code tags, thank you
This User Gave Thanks to cjcox For This Post:
# 3  
Old 03-24-2011
Thank you so very much! I am really grateful. Could you please explain what happens when you give the parameters -k1,1 -k2,2n ?
# 4  
Old 03-24-2011
Quote:
Originally Posted by maq
Thank you so very much! I am really grateful. Could you please explain what happens when you give the parameters -k1,1 -k2,2n ?
-k1,1 tells sort that: first sort the file alphabetically using column 1 as the key. (1,1) means only use the first column.
The option just "-k1" would mean use the whole line as key starting with column 1.

-k2,2n that follows means: after sorting with first column sort with only 2nd column as key (2,2). The option "n" means sort is done numerically using this field.

-G
This User Gave Thanks to gaurab For This Post:
# 5  
Old 03-26-2011
Thank you so much.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

words sort

hello guys i need a command that take the words from multiple files and put them in another file this way: one word needs to appear only once in the destination file with small letters no matter how it appears in source files , the words from destination file needs to be alphabetical ordered and... (10 Replies)
Discussion started by: G30
10 Replies

2. Shell Programming and Scripting

Adding numbers matching with words

Hi All, I have a file which looks like this: abc 1 abc 2 abc 3 abc 4 abc 5 bcd 1 bcd 3 bcd 3 bcd 5 cde 7 This file is just a miniature version of what I really have. Original file is some 1 million lines long. I have tried to come up with the code for what I wish to accomplish... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

3. 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

4. Shell Programming and Scripting

Put numbers against the words

Hi All, I tried to solve this but the result gives me all zeros for one file. I failed to do for all 500 files. I have some 500 files with the extension .dat I have another set of files; 500 in number with extension .dic I created these .dic files by using sort -u from the actual .dat files.... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

5. Shell Programming and Scripting

Difference between words and not between numbers

Hi, Sorry in advance for propably a silly question, but I am a bit lost. On some of the linux job flow I have the following check: if ($file != 1500) then echo ERROR It works ok, all times $file is not equal to 1500 I have the error message. I try to do something similar... (7 Replies)
Discussion started by: essemario
7 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. Shell Programming and Scripting

sort words in a line

Hi Im looking for a way, hopefully a one-liner to sort words in a line e.g "these are the words in a line" to "a are in line the these words" Thanks! (15 Replies)
Discussion started by: rebelbuttmunch
15 Replies

8. Shell Programming and Scripting

Query to print numbers in words

Hi, I have to write a shell script that converts numbers in to words below is what i wrote.My script is not running. ----------------------------------- echo -n "Enter number : " read n len= echo $n | wc -c echo " number in words : " for ( i=1; i<len; i++ ) do num=echo $n... (5 Replies)
Discussion started by: bab123
5 Replies

9. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies

10. Web Development

Query to print numbers in words

Hi, If i give a number say "1234" the output of mysql query should be: one thousand and twenty four How to write mysql query for this? With regards Vanitha (5 Replies)
Discussion started by: vanitham
5 Replies
Login or Register to Ask a Question