Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-11-2009
Registered User
 

Join Date: Feb 2009
Posts: 7
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-
Sponsored Links
  #2 (permalink)  
Old 02-11-2009
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,172

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 (permalink)  
Old 02-11-2009
Registered User
 

Join Date: Mar 2007
Posts: 77
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 (permalink)  
Old 02-11-2009
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,172
Quote:
Originally Posted by gio001 View Post
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 (permalink)  
Old 02-11-2009
Registered User
 

Join Date: Mar 2007
Posts: 77
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 (permalink)  
Old 02-11-2009
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,172

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 (permalink)  
Old 02-11-2009
Registered User
 

Join Date: Feb 2009
Posts: 7
Thumbs up

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

thank u so much!


Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to sort a file and then print similar lines once shira Shell Programming and Scripting 6 01-27-2009 01:22 PM
Sort, group rows patrykxes Shell Programming and Scripting 6 01-06-2009 02:28 PM
awk help required to group output and print a part of group line and original line rajan_san Shell Programming and Scripting 5 10-27-2008 09:16 AM
Sort cmd to get GROUP BY? RacerX UNIX for Dummies Questions & Answers 6 10-16-2008 08:40 AM
PHP & MySQL: sort desending and print perleo Shell Programming and Scripting 1 09-02-2003 12:50 PM



All times are GMT -4. The time now is 07:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0