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 Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-26-2008
Nayanajith Nayanajith is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 27
Column sum group by uniq records

Dear All,

I want to get help for below case.
I have a file like this.


saman 1
gihan 2
saman 4
ravi 1
ravi 2

so i want to get the result,

saman 5
gihan 2
ravi 3 like this.

Pls help me.

Thank you.
  #2 (permalink)  
Old 01-27-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
What have you tried so far?
  #3 (permalink)  
Old 01-27-2008
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 166
This can be done using associative array in awk.

Code:

$ cat nayan.out
saman 1
gihan 2
saman 4
ravi 1
ravi 2

$ awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr[i]}}' nayan.out > nayan.out.tmp

$ cat nayan.out.tmp
ravi 3
saman 5
gihan 2
//Jadu
  #4 (permalink)  
Old 01-28-2008
Nayanajith Nayanajith is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 27
Dear Jadu,

Thank u ! it is working.

Thanks you again,

Nayanajith.
  #5 (permalink)  
Old 02-16-2009
sandeep13 sandeep13 is offline
Registered User
  
 

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

Hi Jadu,

I am new to unix and i have a similar requirement given below:

Input file:

Test.txt
PORT; ID; TOTAL
port1;p1;100000
port2;p2;5000
port1;p1;500

Output file:
PORT; ID; TOTAL
port1;p1; 100500
port2;p2; 5000

How can achive this? Any help on this regard is higly appreciated.
Thanks.

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

Join Date: Nov 2007
Posts: 157
Try below script ( Not tested)

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

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

Join Date: Feb 2009
Posts: 8
Hi Ranjith,
Thanks for the reply but this doesn't work....can we use like arr[$1 ";" $2]???

Regards,
Sandeep
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 06:16 AM.


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