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?
# 8  
Old 11-08-2012
Sorry for the delay, another one:

Code:
awk 'END {
  for (i = 0; ++i <= NR;) {
    split(data[i], t)
    print count[t[1]] "=" data[i]
    }
  }
{ 
  count[$1]++
  data[NR] = $0
  }' infile | 
    sort -k1,1r -k3,3n |
      cut -d= -f2


Last edited by radoulov; 11-08-2012 at 01:25 PM..
This User Gave Thanks to radoulov For This Post:
# 9  
Old 11-08-2012
With Perl:

Code:
perl -ane'
  push @{$data{$F[0]}}, $F[1];
  END {
    for $k (sort { 
              @{$data{$b}} <=> @{$data{$a}} 
              } keys %data) {
      printf "%s %s\n", $k, $_ 
        for sort { 
              $a <=> $b 
              } @{$data{$k}};
      }
    }' infile


Last edited by radoulov; 11-08-2012 at 05:50 PM..
# 10  
Old 11-08-2012
Ok so ironically enough, I found an error with both of these scripts, but they do different things. Both of them start messing things up once there are 10 or more unique words. ex:
using the file:
Code:
file1:
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
ape 6
ape 8
ape 7
ape 9
whale 3

for:
Code:
cut -f1 -d' ' file1|uniq -c|sort -r|cut -d' ' -f8|while read x; do grep "$x" file1|sort -k2; done

the output is:
Code:
dog 1
dog 3
dog 5
dog 9
cat 1
cat 3
cat 7
whale 3
ape 1
cat 1
dog 1
ape 2
ape 2
ape 3
cat 3
dog 3
whale 3
ape 4
ape 4
ape 5
dog 5
ape 6
ape 7
cat 7
ape 8
ape 9
dog 9

and using the same file, with this code:
Code:
awk 'END {
  for (i = 0; ++i <= NR;) {
    split(data[i], t)
    print count[t[1]] "=" data[i]
    }
  }
{
  count[$1]++
  data[NR] = $0
  }' file1 |      sort -k1,1r -k3,3n |       cut -d= -f2

the output is:
Code:
dog 1
dog 3
dog 5
dog 9
cat 1
cat 3
cat 7
whale 3
ape 1
ape 2
ape 2
ape 3
ape 4
ape 4
ape 5
ape 6
ape 7
ape 8
ape 9

Help?
# 11  
Old 11-08-2012
try also:
Code:
awk '
{ c[$1]+=$2; x[NR]=$1; y[NR]=$0; z[NR]=$2; }
END {
  for (i=1;i<=NR;i++) {
    printf "%d %d :%s\n", c[x[i]], z[i], y[i];
  }
}
' file.txt | sort -k1nr,2 | sed 's/.*://'

# 12  
Old 11-08-2012
Yep, sorry.
Fixed below:

Code:
awk 'END {
  for (i = 0; ++i <= NR;) {
    split(data[i], t)
    print count[t[1]] "=" data[i]
    }
  }
{ 
  count[$1]++
  data[NR] = $0
  }' infile | 
    sort -k1,1nr -k3,3n |
      cut -d= -f2

# 13  
Old 11-09-2012
That's the problem with non-fitting samples in the first place, which are extended afterwards thus ruining solutions. Try
Code:
sort file|cut -f1 -d' '|uniq -c|sort -rn|while read y x; do grep "$x" file|sort -k2n; done

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