Sort Data by Group !


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort Data by Group !
# 1  
Old 09-05-2011
Question Sort Data by Group !

Hello,

I have a file and i want to sort by third column and extract the three top lines of each group, it is determined by the second column (144, 89, 55, etc).

Could you please help me with the appropiate awk shell script

Code:
      XLY-XLP   144   0.592772       XLY-XLE   144   0.798121       XLY-XLF   144   0.682582       XLY-XLV   144   0.762279       XLY-XLI   144   0.893199       XLY-XLB   144   0.908553       XLY-XLK   144   0.902004       XLY-XLU   144   0.393566       XLP-^FTSE   89   0.85931       XLP-^N225   89   0.65297       XLP-USO   89   0.778344       XLP-UUP   89   0.464081       XLP-GLD   89   -0.7242       XLP-SLV   89   -0.38121       XLP-GXG   89   0.630136       XLP-FXE   89   -0.05132       XLP-SLV   55   -0.36012       XLP-GXG   55   0.717349       XLP-FXE   55   -0.00732       XLP-EUO   55   0.148117       XLP-KOL   55   0.846785       XLP-HAL   55   0.700265       XLP-ORCL   55   0.856371       XLP-C   55   0.806475

Sincerely,

Carlos
# 2  
Old 09-05-2011
Take the trouble and give example of needed output.
# 3  
Old 09-05-2011
Question

Hello,

I have a file and i want to sort by third column and extract the three top lines of each group, it is determined by the second column (144, 89, 55, etc).

Could you please help me with the appropiate awk shell script


Code:
Data:
XLY-XLP,144,0.592772
XLY-XLE,144,0.798121
XLY-XLF,144,0.682582
XLY-XLV,144,0.762279
XLY-XLI,144,0.893199
XLY-XLB,144,0.908553
XLY-XLK,144,0.902004
XLY-XLU,144,0.393566
XLP-^FTSE,89,0.85931
XLP-^N225,89,0.65297
XLP-USO,89,0.778344
XLP-UUP,89,0.464081
XLP-GLD,89,-0.724198
XLP-SLV,89,-0.381205
XLP-GXG,89,0.630136
XLP-FXE,89,-0.0513242
XLP-SLV,55,-0.360124
XLP-GXG,55,0.717349
XLP-FXE,55,-0.00732295
XLP-EUO,55,0.148117
XLP-KOL,55,0.846785
XLP-HAL,55,0.700265
XLP-ORCL,55,0.856371
XLP-C,55,0.806475

Output required:
XLY-XLB,144,0.908553
XLY-XLK,144,0.902004
XLY-XLI,144,0.893199
XLP-^FTSE,89,0.85931
XLP-USO,89,0.778344
XLP-^N225,89,0.65297
XLP-ORCL,55,0.856371
XLP-KOL,55,0.846785
XLP-C,55,0.806475

Sincerely,

Carlos
# 4  
Old 09-05-2011
Code:
sort -t, -k2,2nr -k3,3nr infile | 
  awk -F, '_[$2]++ < 3'

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 group similar data?

Hello everybody: I want rearrange about 5 million rows (with 300 columns) into groups. Data looks like the following: where there were various experiments (column 2) conducted at different locations (column headers in top row column 4 onwards) in different years (column 1) using instruments... (0 Replies)
Discussion started by: sheetalk
0 Replies

2. Shell Programming and Scripting

De-group data

Hi, some help is highly appreciated, I want to de-group my data for statistical analysis. I made up some sample data, there shouldnt be repeated lines in the output. My data is in excel but I can make it tab-delimited text. A B,C A B,D,E X Y X Y,ZExpected output A B A C A D A E X Y... (3 Replies)
Discussion started by: ritakadm
3 Replies

3. UNIX for Advanced & Expert Users

Group/Sort logs by thread number

Hello Experts, For the following sample log, can you tell me how I can sort it or group it by thread number. To be specific, I just want to group them together so that its easy to analyize. Thanks. 2012-08-17 00:00:06,369 INFO ExecuteThread: '33' for queue:... (2 Replies)
Discussion started by: samjna
2 Replies

4. Shell Programming and Scripting

Sort the file contents in each group....print the group title as well

I've this file and need to sort the data in each group File would look like this ... cat file1.txt Reason : ABC 12345-0023 32123-5400 32442-5333 Reason : DEF 42523-3453 23345-3311 Reason : HIJ 454553-0001 I would like to sort each group on the last 4 fileds and print them... (11 Replies)
Discussion started by: prash184u
11 Replies

5. Shell Programming and Scripting

Split file by data group

Hi all, I'm having a little trouble solving a file split I need to get done. I have the following data: 1. Light 1A. Light Soft texture: it's soft color: the color value is that of something light vital statistics: srm: 23 og: 1.035 sp: 1.065 comment: this is nice if you like... (8 Replies)
Discussion started by: mkastin
8 Replies

6. Shell Programming and Scripting

AWK or KSH : Sort, Group and extract from 3 files

Hi, I've the following two CSV files: File1.csv File2.csv Class,Student# Student#,Marks 1001,6001 6002,50 1001,6002 6001,60 1002,7000 ... (3 Replies)
Discussion started by: Matrix2682
3 Replies

7. UNIX for Dummies Questions & Answers

sort group of n lines base on pattern in first line

I have record having 10 fields and each field being printed on a new line, first line cotains name of exchange, 2nd line stock name, third line stock price, etc etc... now i want to retrieve data only for a particular exchanged and that too only 2nd and 3rd row info... NSE RNRL 70 12 1... (1 Reply)
Discussion started by: manishma71
1 Replies

8. Shell Programming and Scripting

awk - sort, then print the high value for each group

Hi @ all I'm trying to achive to this problem, I've a 2-column composed file as the following: 192.168.1.2 2 192.168.1.3 12 192.168.1.2 4 192.168.1.4 3 cpc1-swan1-2-3-cust123.swan.cable.ntl.com 4 192.168.1.3 5 192.168.1.2 10 192.168.1.4 8... (8 Replies)
Discussion started by: m4rco-
8 Replies

9. Shell Programming and Scripting

Sort, group rows

I wrote script in bash which generates this report: User1,admin,rep,User2,shell,path1,x1,r1 User2,admin,rep,User7,shell,path1,x1,r1 User3,admin,rep,User4,shell,path1,x1,r1 User4,admin,rep,User3,shell,path1,x1,r1 User5,admin,rep,User1,shell,path1,x1,r1 User6,admin,rep,User5,shell,path1,x1,r1... (6 Replies)
Discussion started by: patrykxes
6 Replies

10. UNIX for Dummies Questions & Answers

Sort cmd to get GROUP BY?

Is there any way using the sort command to get something like a GROUP BY clause? I'm trying to sort through race_event records and group them by those individuals that raced together in the same race on the same date at the same track, but i run into a problem when there were days when the track... (6 Replies)
Discussion started by: RacerX
6 Replies
Login or Register to Ask a Question