The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Monkcast #12: IBM HW group OEMs Solaris to chagrin of SW group & a ... - ZDNet.com bl iBot UNIX and Linux RSS News 0 08-17-2007 04:30 PM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 12:15 PM
find and group records in a file thumsup9 UNIX for Advanced & Expert Users 20 04-19-2007 05:04 PM
Select records based on search criteria on first column shashi_kiran_v UNIX for Dummies Questions & Answers 2 12-02-2005 01:49 PM
entry in /etc/group too long - problem using sudo with %group poli SUN Solaris 4 12-21-2004 09:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-16-2009
ranjithpr ranjithpr is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 157
Try this

Code:
awk -F  ";"  '{ string=$1 ";" $2; arr[string] += $3 } END {for (i in arr) {print i ";" arr[i] } }' inputfile

Last edited by Franklin52; 02-16-2009 at 06:13 AM.. Reason: adding code tags
  #2 (permalink)  
Old 02-16-2009
sandeep13 sandeep13 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 8
Column sum group by uniq records Reply to Thread

This one also doesn't work....
  #3 (permalink)  
Old 02-16-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
What doesn't work? Did you get errors, wrong output, no output? Try this:

Code:
awk 'BEGIN{FS=OFS=";"}
NR==1{print;next}
{a[$1";"$2]+=$3}
END{for(i in a)print i, a[i]}' file
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
  #4 (permalink)  
Old 02-16-2009
sandeep13 sandeep13 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 8
Hi Franklin,

I tried ur solution and getting below o/p

PORT;PID;TOTAL
port1;p1
port2;p2

I guess someting is missing, it is doing group by but missing the output values.
Thanks for the response.

Regards,
Sandeep
  #5 (permalink)  
Old 02-16-2009
sandeep13 sandeep13 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 8
Column sum group by uniq records Reply to Thread

getting below o/p..missed some values in last post

PORT;PID;TOTAL
port1;p1;500
port2;p2;0


Quote:
Originally Posted by sandeep13 View Post
Hi Franklin,

I tried ur solution and getting below o/p

PORT;PID;TOTAL
port1;p1
port2;p2

I guess someting is missing, it is doing group by but missing the output values.
Thanks for the response.

Regards,
Sandeep
  #6 (permalink)  
Old 02-16-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
This is what I get:

Code:
$ cat file
PORT; ID; TOTAL
port1;p1;100000
port2;p2;5000
port1;p1;500
$
$
$ awk 'BEGIN{FS=OFS=";"}                              
NR==1{print;next}
{a[$1";"$2]+=$3}
END{for(i in a)print i, a[i]}' file
PORT; ID; TOTAL
port2;p2;5000
port1;p1;100500
$
$
Regards
  #7 (permalink)  
Old 02-16-2009
sandeep13 sandeep13 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 8
Column sum group by uniq records Reply to Thread

HI Franklin,

Thanks a lot. It works....using nawk

/usr/bin/nawk 'BEGIN{FS=OFS=";"}NR==1{print;next}{a[$1";"$2]+=$3}END{for(i in a)print i, a[i]}' file

Much appreciated.

Cheers,
Sandeep


Quote:
Originally Posted by Franklin52 View Post
This is what I get:

Code:
$ cat file
PORT; ID; TOTAL
port1;p1;100000
port2;p2;5000
port1;p1;500
$
$
$ awk 'BEGIN{FS=OFS=";"}                              
NR==1{print;next}
{a[$1";"$2]+=$3}
END{for(i in a)print i, a[i]}' file
PORT; ID; TOTAL
port2;p2;5000
port1;p1;100500
$
$
Regards
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 On




All times are GMT -4. The time now is 12:12 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-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0