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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - sort, then print the high value for each group
# 1  
Old 02-11-2009
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
cpc1-swan1-2-3-cust123.swan.cable.ntl.com 8

and i would this kind of output :

192.168.1.2 10
192.168.1.3 12
192.168.1.4 8
cpc1-swan1-2-3-cust123.swan.cable.ntl.com 8

I'm trying to use this methodology :

Code:
awk  '{  a[$NF]=$2; if ( a[$NF] >= max[$NF] ) max[$NF]=a[$NF] } END { for ( item in a )  print item,max[item] }'


but i'm not sure this is the right way....

Thanks you all in advance!!!

m4rco-
# 2  
Old 02-11-2009
Code:
nawk '{
  max[$1] = !($1 in max) ? $2 : ($2 > max[$1]) ? $2 : max[$1]
}
END {
  for (i in max)
    print i, max[i]
}' myFile

# 3  
Old 02-11-2009
How about this .....

awk '{print $2" "$1}' file_in_input | sort -k 2 -k 1rn | uniq -f 1 |awk '{print $2" "$1}' >output_file

Hope this is what you needed.
G.
# 4  
Old 02-11-2009
Quote:
Originally Posted by gio001
How about this .....

awk '{print $2" "$1}' file_in_input | sort -k 2 -k 1rn | uniq -f 1 |awk '{print $2" "$1}' >output_file

Hope this is what you needed.
G.
way too many unnecessary 'pipes' for my taste....
# 5  
Old 02-11-2009
Hello,
I am amazed of how smooth the code is ...can you explain a little in words what is the logic of the line:

max[$1] = !($1 in max) ? $2 : ($2 > max[$1]) ? $2 : max[$1]

Thanks.

Last edited by gio001; 02-11-2009 at 04:39 PM..
# 6  
Old 02-11-2009
Code:
max[$1] = !($1 in max) ? $2 : ($2 > max[$1]) ? $2 : max[$1]

max[$1] - 'max' is an array indexed by the value of the FIRST field
!($1 in max) ? $2 - if '$1' is NOT already in array 'max', return the value of the SECOND field
: - otherwise
($2 > max[$1]) ? $2 : max[$1]
if the value of the SECOND field is greater than what's already in array 'max' (index by '$1'), return the value of the SECOND field. If not, return the current value of array 'max[$1]'.

'max[$1] =' - whatever gets returned, store it array 'max' index by the FIRST field.

Lil' bit wordy, but I hope you get the gist of it.

Last edited by vgersh99; 02-12-2009 at 07:31 AM..
# 7  
Old 02-11-2009
MySQL

wow vgers...
amazing solution, and amazing explaination.

thank u so much!


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

AWK exclude first and last record, sort and print

Hi everyone, I've really searched for a solution to this and this is what I found so far: I need to sort a command output (here represented as a "cat file" command) and from the second down to the second-last line based on the second row and then print ALL the output with the specified section... (7 Replies)
Discussion started by: dentex
7 Replies

4. Shell Programming and Scripting

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 XLY-XLP 144 0.592772 XLY-XLE 144 0.798121 ... (3 Replies)
Discussion started by: csierra
3 Replies

5. 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

6. Shell Programming and Scripting

Need to print if the memory utiliztion is high or less than 10G

top -n 0 | head -4 | tail -1 | awk -F "," '{ if ( $2 < 10G ) { print " High Memory utilization " } else { print " " } }' 23n01 labc> ./b.sh High Memory utilization This prints hifgh memory utilization even though there is enough memory. Memory: 32G real, 19G free, 11G swap in use, 45G... (0 Replies)
Discussion started by: arch12
0 Replies

7. 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

8. 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

9. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 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