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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Columns to rows mgirinath Shell Programming and Scripting 16 11-29-2007 07:03 PM
Cut rows obedkhan HP-UX 2 08-23-2006 09:36 AM
Finding different rows dombi Shell Programming and Scripting 2 03-06-2006 05:01 PM
Difference between two rows JimJim Shell Programming and Scripting 7 07-05-2005 07:52 AM
Concatenate 2 rows into 1 row indianadoug Shell Programming and Scripting 4 03-11-2005 11:05 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 08-05-2008
i.scientist i.scientist is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 25
sum of all matching rows using awk

I have file
"1","x1897"," 89.10"
"1","x2232"," -12.12"
"1","x1897"," 389.10"
"1","x2232"," 212.12"
"1","x1897"," 19.10"
"1","x2232"," 2.12"


i want to add all 3 rd column rows (they have spaces also)for x1
and sum of 3rd column rows for x2 separately.
I am very bad at awk. can some one help me ?
  #2 (permalink)  
Old 08-05-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
What have you tried so far?

This is not homework for a class is it???
  #3 (permalink)  
Old 08-05-2008
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
Code:
awk -F'"' '/x1/{a+=$(NF-1)}/x2/{b+=$(NF-1)}END{print "x1-> " a, " x2-> " b}' file

x1-> 497.3  x2-> 202.12
or with a new line in between

Code:
awk -F'"' '/x1/{a+=$(NF-1)}/x2/{b+=$(NF-1)}END{print "x1-> " a, "\nx2-> " b}' file

x1-> 497.3
x2-> 202.12
If the file has more than three fields, use ...

Code:
awk -F'","' '/x1/{a+=$3+0}/x2/{b+=$3+0}END{print "x1-> " a, "\nx2-> " b}' file

Last edited by rubin; 08-06-2008 at 06:27 PM.. Reason: added final code
  #4 (permalink)  
Old 08-05-2008
i.scientist i.scientist is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 25
this is not homework problem..

this is what i did

while read file
do
var=`echo $file | cut -f 3 -d "," | sed
....
....
done < file

but i donot wantt to use while loops


-------------------
thanks for the replies. Will this work if the sum amount are in 13 th column or somewhere else ? I could not find them working...

will this work if we need to find the sum on 13th field. File may contain around 30 fields
sum2.awk

{
gsub(/\"| +/,"",$13)
sum += $13

}
END {
print "Sum: ", sum;
}


$ awk -f sum2.awk file

Last edited by i.scientist; 08-05-2008 at 11:57 PM..
  #5 (permalink)  
Old 08-06-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
No, it wouldn't work, because awk does not know what character to use as a field separator, so it does not know what you mean when you refer to $13.

You can specify a regexp as a separator:

Code:
awk -F '[",]+ *' '{ sum+=$14 } END { print "Sum: ",sum }' file
I had to increase the field number by 1 because with this solution awk would treat the first " as a field delimiter and presume that the first field was empty.
  #6 (permalink)  
Old 08-06-2008
i.scientist i.scientist is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 25
Thanks,

this works fine if all the columns are included in " "
In my case they are not. Sorry for not telling that.

"1",,,,,"X1211","1",

there may be empty fields seperated by commas.
If so how to change the field seperator and how to obtain the sum of 13th column ?
  #7 (permalink)  
Old 08-06-2008
i.scientist i.scientist is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 25
awk -F '[,]' '{ gsub(/\"| +/,"",$13);sum+=$13 } END { printf "Sum: 0.2%d",sum }' file

this worked. Now can i get sum of only those rows matching X1 and X2 ?

thanks for all your replies

Last edited by i.scientist; 08-06-2008 at 10:55 AM..
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 02:42 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