Sort with respect to count


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort with respect to count
# 1  
Old 04-08-2010
Sort with respect to count

Hello!
I have a file with 4 columns. I am trying to have it sort first with respect to the first column, and then with respect to the number of counts (in descending count) in the second column within the same first column identity.

For example:

Input:
1 2 A 1
1 6 B 2
2 5 G 7
1 6 D 4
1 6 F 6
1 0 E 5
2 4 H 8
2 5 I 9
1 2 C 3

The output is to look like:
1 6 B 2
1 6 D 4
1 6 F 6
1 2 A 1
1 2 C 3
1 0 E 5
2 5 G 7
2 5 I 9
2 4 H 8

As you can see:
-Within the first column identity=1, there are 3 counts of '6', 2 counts of '2' and 1 count of '0' in the second column.
-Within the first column identity=2, there are 2 counts of '5' and 1 count of '4' in the second column.
They are both sorted with respect to descending count in the second column.

I have been doing this in excel and it became quite time consuming for me since the number of files to process has started to increase.

Thanks for your help in advance!

Last edited by anchuz; 04-08-2010 at 03:09 PM..
# 2  
Old 04-08-2010
Code:
sort -n -k1 -k2 inputfile > outputfile

# 3  
Old 04-08-2010
The sort in the second column is not (necessarily) in arithmetic order, it is rather based on the reverse count.

Thanks!

Last edited by anchuz; 04-08-2010 at 03:24 PM..
# 4  
Old 04-08-2010
You changed your output sample Smilie
Code:
#!/bin/ksh

 rm *.tmp
 sort -n -k1 filename |  \
   awk '{file=sprintf("%d.tmp", $1); print $0 > file}'
 
 for fname in *.tmp
 do 
    sort -n -r -k2 $fname
 done > outputfile
 rm *.tmp

Note - do this in a small subdirectory it uses tempfiles. If your first column has too many distinct values it will not work either. This depends on system's your open file limit. You'll have to resort to something else.

Code:
getconf OPEN_MAX

tells you how many open files you can have at one time.
# 5  
Old 04-08-2010
Sorry; I just wanted to make it more clear... (I guess I could not succeed =( )

Thanks for the code... However, it still sorts the second column in arithmetic order, not with respect to the number of counts each item has within each group...

I hope the following makes it a little more clear:
Input:
1 2 A 1
1 1 B 2
2 4 G 7
1 1 D 4
1 1 F 6
1 0 E 5
2 5 H 8
2 4 I 9
1 2 C 3

The output is to look like:
1 1 B 2
1 1 D 4
1 1 F 6
1 2 A 1
1 2 C 3
1 0 E 5
2 4 G 7
2 4 I 9
2 5 H 8

Thanks again!
# 6  
Old 04-08-2010
Code:
1 1 B 2
1 1 D 4
1 1 F 6
1 2 A 1
1 2 C 3
1 0 E 5
2 4 G 7
2 4 I 9
2 5 H 8

This is not in any order I understand - with regard to column #2
# 7  
Old 04-08-2010
1) Within the first column identifier = 1:
There are *3* counts of '1',
*2* counts of '2',
*1* count of '0'

2) Within the first column identifier = 2:
There are *2* counts of '4',
*1* count of '5'

They are ordered with respect to the number of counts of each item within the group, starting with the item with the most count to the least, independent of their values...

Last edited by anchuz; 04-08-2010 at 05:09 PM..
 
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

Getting the most common column with respect another

hi all, i want to get the most comon column w.r.t another this is my file Tom|london Tom|london Tom|Paris Adam|Madrid Adam|NY the Output to get me : Tom|london Adamn|Madrid ive tried (10 Replies)
Discussion started by: teefa
10 Replies

3. Shell Programming and Scripting

Advanced: Sort, count data in column, append file name

Hi. I am not sure the title gives an optimal description of what I want to do. Also, I tried to post this in the "UNIX for Dummies Questions & Answers", but it seems no-one was able to help out. I have several text files that contain data in many columns. All the files are organized the same... (14 Replies)
Discussion started by: JamesT
14 Replies

4. Shell Programming and Scripting

cut with delimiter respect text

Hi, Can someone help me to get the shortest command Input file ========= I|know|"english|french" It|can|have|four|delimiters Desired output =========== "english|french" have If I use cut -d "|" -f3 , i am getting "english as 3rd field.But I would like to get the whole text in... (2 Replies)
Discussion started by: anandapani
2 Replies

5. Shell Programming and Scripting

count characters in multiple files and sort

I am trying to count a special character in several thousand files and sort the result according to the number the character occured in each files. files: 1.txt 2.txt 3.txt files look like: ABCDEFGHABBBACF I want the number of B in each file and a result file that lists the occurence... (4 Replies)
Discussion started by: patdoor
4 Replies

6. Shell Programming and Scripting

Perl - Sort and Count foreach line

Can any one please help I am trying to sort each item in every line and count the common (non case sensitive) and at the end printing all the unique alphabetically. Here is what i did ... I can print all the lines but struck to sort each line some were: I am getting the output as: I... (5 Replies)
Discussion started by: sureshcisco
5 Replies

7. Shell Programming and Scripting

Beginner: Count & Sort Using Array's

Hi, I'm new to linux & bash so please forgive my ignorance, just wondering if anyone can help. I have a file (mainfile.txt) with comma deliminated values, like so: $1 $2 $3 613212, 36, 57 613212, 36, 10 613212, 36, 10 677774, 36, 57 619900, 10, 10 i need to split this file... (12 Replies)
Discussion started by: BigTOE
12 Replies

8. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

9. Shell Programming and Scripting

How to sort by count

Hello! I have a file with 4 columns. I am trying to have it sort first with respect to the first column, and then with respect to the number of counts (in descending count) in the second column within the same first column identity. For example: Input: 1 2 A 1 1 6 B 2 2 5 G 7 1 6 D 4 1... (1 Reply)
Discussion started by: anchuz
1 Replies

10. Shell Programming and Scripting

Sort and count using AWK

Hi, I've a fixed width file where I need to count the number of patterns from each line between characters 1 to 15 . so can we sort them and get a count for each pattern on the file between 1 to 15 characters. 65795648617522383763831552 410828003265795648 6175223837... (5 Replies)
Discussion started by: rudoraj
5 Replies
Login or Register to Ask a Question