How to sort the number of occurrences


 
Thread Tools Search this Thread
Operating Systems Linux How to sort the number of occurrences
# 1  
Old 03-03-2011
How to sort the number of occurrences

[IMG]
Code:
file:///C:/Users/TSHEPI%7E1.LEB/AppData/Local/Temp/moz-screenshot.png[/IMG]ATM@ubuntu:~$ cat numbers2 | sort -n | uniq -c
      1
      7 1
     11 2
     10 3

the 1st numbers are the counts from the command "uniq -c", which represent the number of occurrences of each in the file. The "sort -n" sorts the numbers. I need to sort the numbers in the 1st coloumn into descending order.
Please help!

Last edited by joeyg; 03-03-2011 at 11:37 AM.. Reason: Please use CodeTags around commands and output
# 2  
Old 03-03-2011
This?

Code:
$ cat sample1.txt
1
7
11
10
11
10
10

$ cat sample1.txt | sort -nr | uniq -c
      2 11
      3 10
      1 7
      1 1

---------- Post updated at 10:43 AM ---------- Previous update was at 10:42 AM ----------

Then...

Code:
$ cat sample1.txt | sort -n | uniq -c | sort -nr
      3 10
      2 11
      1 7
      1 1

# 3  
Old 03-03-2011
@joeyg, that sorts the contents of the file which is "1, 2 and 3", and not the count amounts.
ATM@ubuntu:~$ cat numbers2 | sort -n | uniq -c
1
7 1
11 2
10 3
sort the 1st coloumn which is the count of the 1,2, and 3 occurrences.
# 4  
Old 03-03-2011
Quote:
Originally Posted by lebogot
[IMG]
[CODE]file:///C:/Users/TSHEPI%7E1.LEB/AppData/Local/Temp/moz-screenshot.png[/IMG]
The internet is not your hard drive, screenshots don't work that way...
# 5  
Old 03-03-2011
Take another look at my 2nd command. That sorts based on the count.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to strictly grep (with before and after args) for last N number of occurrences?

Here is my grep string to print only the last occurrence, with before and after lines. Note that the tail Argument is sum of B and A args + 1, so that it prints the data of only the last 1 match. Now I need to print last 2 such matches. I thought of doubling the tail arg like 5+5+1 (For -- line),... (2 Replies)
Discussion started by: samjna
2 Replies

2. Shell Programming and Scripting

Count the number of string occurrences to display 0 entries in output

Hello Friends, Can somebody assist an issue I am having? I have a separate file with a list of account ids XXX200B02Y01 XXX200B03Y01 XXX200B05Y01 XXX200B07Y01 XXX200B08Y01 I call the file, and run an egrep against a directory and logfiles AccountID=$(cat... (2 Replies)
Discussion started by: liketheshell
2 Replies

3. Shell Programming and Scripting

Script to Serach pattern and give number of occurrences

Hi, I want a script which search for a pattern "good" in a huge file and provide me number of occurences of such pattern in a file. lets say i have a file test.txt contents as below good is good but good is sometime bad and sometime good you are very good and good is always good ... (7 Replies)
Discussion started by: sv0081493
7 Replies

4. Shell Programming and Scripting

How to search number of occurrences of a particular string in a file through vi editor?

i have one file, i am doing 'vi Filename' now i want to search for particular string and i want to know how many times that string occurs in whole file (5 Replies)
Discussion started by: sheelsadan
5 Replies

5. Shell Programming and Scripting

Help with Unix and Awk to count number of occurrences

Hi, I have a file (movies.sh), this file contains list of movies such as I want to redirect the movies from movies.sh to file_to_process to allow me process the file with out losing anything. I have tried Movies.sh >> file_to_process But I want to add the row number to the data... (2 Replies)
Discussion started by: INHF
2 Replies

6. Shell Programming and Scripting

PERL : Sort substring occurrences in array of strings

Hi, My developer is on vacation and I am not sure if there is something which is easier for this. I have an array of strings. Each string in the array has "%" characters in it. I have to get the string(s) which have the least number of "%" in them. I know how I can get occurrences : ... (7 Replies)
Discussion started by: sinpeak
7 Replies

7. Shell Programming and Scripting

counting number of pattern occurrences

Hi All, Is it possible to count number of occurrences of a pattern in a single record using awk?? for example: a line like this: abrsjdfhafa I want to count the number of a character occurrences. but still use the default RS, I don't want to set RS to single character. (1 Reply)
Discussion started by: ghoda2_10
1 Replies

8. UNIX for Dummies Questions & Answers

Print number of occurrences of many different strings

People, I need your help with making a script which will 1. take as an input the number of lines, smth like this: ((RUBROBACTER_1_PE1288 (((SALINISPORA_1_PE1863 SALINISPORA_1_PE1828)100 ((NOCARDIOIDES_2_PE2419 PROPIONIBACTERIUM_1_PE1395)96 ((((((((CORYNEBACTERIUM_1_PE1119... (3 Replies)
Discussion started by: roussine
3 Replies

9. Shell Programming and Scripting

Count the number of occurrences of the word

I am a newbie in UNIX shell script and seeking help on this UNIX function. Please give me a hand. Thanks. I have a large file. Named as 'MyFile'. It was tab-delmited. I am told to write a shell function that counts the number of occurrences of the ord “mysring” in the file 'MyFile'. (1 Reply)
Discussion started by: duke0001
1 Replies

10. Programming

output the letters of the alphabet with the number of occurrences

hi, I'm trying to create a program that will read a file and then check the file for each letter of the alphabet and then output the letter and the number of times it appears in the file, into a new file... this is what i have so far but it's not working.. if anyone could help that would be nice!... (10 Replies)
Discussion started by: svd
10 Replies
Login or Register to Ask a Question