Sort the file based on number of occurences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort the file based on number of occurences
# 1  
Old 08-09-2013
Sort the file based on number of occurences

I have a file (input) I want to sort the file based on the number of times a pattern in the first column occurs for example grapes occurs 4 times in combination with other patterns so i want it to be first like shown in the output file. then apple ocuurs thrice so it occupies second position and so on

input
Code:
apple	banana
apple	apple
apple	grapes
grapes	banana
grapes	melon
grapes	orange
grapes	cherry
orange	apple
orange	banana
banana	cherry


output
Code:
grapes	banana
grapes	melon
grapes	orange
grapes	cherry
apple	banana
apple	apple
apple	grapes
orange	apple
orange	banana
banana	cherry

# 2  
Old 08-09-2013
Brute force:
Code:
awk '{a[$1]++}END{for (i in a) {print i,a[i]}}' data|sort -k2 -nr|awk '{print $1}'|xargs -i ksh -c 'grep "^{}" data'

# 3  
Old 08-09-2013
@Klashxx
Something goes wrong here

Code:
cat data
apple   banana
apple   apple
apple   grapes
grapes  banana
grapes  melon
grapes  orange
grapes  cherry
orange  apple
orange  banana
banana  cherry

Code:
awk '{a[$1]++}END{for (i in a) {print i,a[i]}}' data|sort -k2 -nr|awk '{print $1}'|xargs -i ksh -c 'grep "^{}" data'
xargs: ksh: No such file or directory

# 4  
Old 08-09-2013
@Jotne try to use an absolute path.
Ex:
Code:
xargs -i /bin/ksh -c 'grep "^{}" data'

or:
Code:
xargs -i /bin/sh -c 'grep "^{}" data

Tested in redhat 6.3
# 5  
Old 08-09-2013
Did not have kshell on my ubuntu, bash and sh works fine Smilie
# 6  
Old 08-09-2013
Code:
#!/usr/bin/perl
use strict;
use warnings;

my %fruit_salad;
while(<DATA>){
    chomp;
    my ($main,$side)=split/\s+/,$_;
    push @{$fruit_salad{$main}},$side;
}
for my $main (sort {scalar @{$fruit_salad{$b}} <=> scalar @{$fruit_salad{$a}} } keys %fruit_salad ){
    for my $side (@{$fruit_salad{$main}}){
         print "$main\t$side\n";
    }
}



__DATA__
apple   banana
apple   apple
apple   grapes
grapes  banana
grapes  melon
grapes  orange
grapes  cherry
orange  apple
orange  banana
banana  cherry

seems to work OK
# 7  
Old 08-09-2013
An awk solution
Code:
awk '{if(!x[$1])x[$1]=++y;z=x[$1];a[z]++;b[z]=b[z]","$2;m[z]=$1}
END{n=asort(a,d);for(i=n;i>0;i--){for(j in a){if(a[j]==d[i]){split(b[j],c,",");f=2;for(k=a[j];k>0;k--)print m[j],c[f++];a[j]=-1}}}}' input_file

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort file based on number of delimeters in line

Hi, Need to sort file based on the number of delimeters in the lines. cat testfile /home/oracle/testdb /home /home/oracle/testdb/newdb /home/oracle Here delimeter is "/" expected Output: /home/oracle/testdb/newdb /home/oracle/testdb /home/oracle /home (3 Replies)
Discussion started by: Sumanthsv
3 Replies

2. Shell Programming and Scripting

Number of Consecutive Occurences

:b: Hi Folks, I have a requirement to find the number of consecutive occurences based on month. Here is the below sample of my requirement. I have say four columns CARUSERNAME BRAND_CODE MONTH YEAR Below is sample Data John|C|JAN|2013 Tim|C|FEB|2013 Tom|C|Mar|2013 Ron|C|Apr|2013... (4 Replies)
Discussion started by: dinesh1985
4 Replies

3. Shell Programming and Scripting

Count and print the number of occurences

I have some files as shown below GLL ALM 654-656 654 656 SEM LYG 655-657 655 657 SEM LYG 655-657 655 657 ALM LEG 656-658 656 658 ALM LEG 656-658 656 658 ALM LEG 656-658 656 658 LEG LEG 658-660 658 660 LEG LEG 658-660 658 660 The value of GLL is... (5 Replies)
Discussion started by: arch
5 Replies

4. UNIX for Dummies Questions & Answers

Sort Files based on the number(s) on the file name

Experts I have a list of files in the directory mysample1 mysample2 mysample3 mysample4 mysample5 mysample6 mysample7 mysample8 mysample9 mysample10 mysample11 mysample12 mysample13 mysample14 mysample15 (4 Replies)
Discussion started by: dsedi
4 Replies

5. Shell Programming and Scripting

Count occurences based on interval

Hi, I have a file which has 4500 entries 10000 9880 9800 8700 8200 ... ..... ... ... ... ... ... ... ... 50 (1 Reply)
Discussion started by: Diya123
1 Replies

6. UNIX for Dummies Questions & Answers

Number of word occurences in a file?

Hello world, Can anybody tell me how to count how many times does a word repeat in a file? There have been many threads on this but they all are heavy loads of Scripting for a starter like me. :D So, I sat down today and after some hours of reading man pages, I found a simple one-line... (18 Replies)
Discussion started by: satish51392111
18 Replies

7. Shell Programming and Scripting

awk counting number of occurences

Hi, I am trying to count the max number of occurences of field1 in my apache log example: 10.0.0.1 field2 field3 10.0.0.2 filed2 field3 10.0.0.1 field2 field3 10.0.0.1 field2 field3 awk result to print out only the most occurence of field1 and number of occurence and field1 is... (3 Replies)
Discussion started by: phamp008
3 Replies

8. Shell Programming and Scripting

counting the number of occurences

say i've got a text file with >10million sequences: ssss ssss tttttt uuuuuu uuuuuu uuuuuu ... I'd like to convert the file so that the output will report the number of occurence right by each sequence: 2 ssss 2 ssss 1 tttttt 3 uuuuuu 3 uuuuuu 3 uuuuuu .... (3 Replies)
Discussion started by: johjoh
3 Replies

9. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 Replies

10. Shell Programming and Scripting

number of occurences of a string

hi, I have a file where i need to count the occurences of a string ex) 'welcome to unix forum'. can anybody help me out (12 Replies)
Discussion started by: siddu_chittari
12 Replies
Login or Register to Ask a Question