How can I sort by n number is like words?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I sort by n number is like words?
# 1  
Old 11-08-2012
How can I sort by n number is like words?

I want to sort a file with a list of words, in order of most occuring words to least occurring words as well as alphabetically.

ex:
Code:
file1:
cat 3
cat 7
cat 1
dog 3
dog 5
dog 9
dog 1
ape 4
ape 2

I want the outcome to be:
Code:
file1.sorted:
dog 1
dog 3
dog 5
dog 9
cat 1
cat 3
cat 7
ape 2
ape 4

So far all I know how to do is sort it alphabetically:
Code:
sort file1 file1.sorted

any help please?
# 2  
Old 11-08-2012
Code:
sort -k1,1r -k2,2n infile

# 3  
Old 11-08-2012
That worked for the example I gave but I do not believe it does what I want it to do, I may have mislead my question by having my outcome be in reverse alphabetical order. That is only a coinsodence, I do not want it in reverse order. let me add to the example:

Code:
file1:
cat 3
cat 7
cat 1
dog 3
dog 5
dog 9
dog 1
ape 4
ape 2
whale 3

I want the outcome to be:

Code:
dog 1
dog 3
dog 5
dog 9
cat 1
cat 3
cat 7
ape 2
ape 4
whale 3

# 4  
Old 11-08-2012
Looong pipe chain, but avoiding awk:
Code:
$ cut -f1 -d' ' file1|uniq -c|sort -r|cut -d' ' -f8|while read x; do grep "$x" file1|sort -k2; done
dog 1
dog 3
dog 5
dog 9
cat 1
cat 3
cat 7
ape 2
ape 4
whale 3

You may want to sort your file1 first so uniq will work properly.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-08-2012
wow that worked, however, I ran into some trouble again. I added the word "ape" and extra few times and it did not give me the outcome I want:

Code:
cat 3
cat 7
cat 1
dog 3
dog 5
dog 9
dog 1
ape 4
ape 2
ape 1
ape 2
ape 3
ape 4
ape 5
whale 3

the outcome should have been:

Code:
ape 1
ape 2
ape 2
ape 3
ape 4
ape 4
ape 5
dog 1
dog 3
dog 5
dog 9
cat 1
cat 3
cat 7
whale 3

# 6  
Old 11-08-2012
... which is exactly the output I get when using your new input file. What do you get?
# 7  
Old 11-08-2012
nevermind, I typed it wrong, it did work! thanks alot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

3. Shell Programming and Scripting

How to sort lines according words?

Hello I greped some lines from an xml file and generated a new file. but some entries are missing my table is unsorted. e.g. NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" NUMBER="2" POSTCODE="33333"... (5 Replies)
Discussion started by: witchblade
5 Replies

4. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

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

6. UNIX for Dummies Questions & Answers

Trying to sort words and numbers associated with them.

Hi. I have a file containing words and numbers associated with them as follows - 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 (4 Replies)
Discussion started by: maq
4 Replies

7. UNIX for Dummies Questions & Answers

number of words in delimited string

Hi, I'm looking for one liner code for counting number of words in a delimited string.. I know about wc -w ..but if i give wc -w a.txt,b.txt it won't work with delimited sting as it was looking for files with a.txt and b.txt I know there will be a simple solution..i couldn't... (5 Replies)
Discussion started by: ammu
5 Replies

8. Shell Programming and Scripting

how to find number of words

please help me for this "divide the file into multiple files containing no more than 50 lines each and find the number of words of length less than 5 characters" (3 Replies)
Discussion started by: annapurna konga
3 Replies

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

10. Programming

how i display number in words

helo i want to implement the following concept in my project write a c/c++ algorithm for : accept a number from the user not greater than 6 digits and display the number in words i.e. if the input from the user is 18265 then the output should be Eighteen Thousand Two Hundred Sixty Five. if the... (3 Replies)
Discussion started by: amitpansuria
3 Replies
Login or Register to Ask a Question