Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-17-2012, 10:20 AM
|
#1
|
LQ Newbie
Registered: Feb 2012
Posts: 26
Rep: 
|
summing by a particular column
hi there i have a huge file
Quote:
SUBSCRIPTION QUOTA Balance Used
WIMAXPOSTMONTHLY3GB 3,145,728 48,293,568 -
WIMAXPOSTMONTHLY10GB 10,485,760 15,914,810 36,514,022
WIMAXPOSTMONTHLY10GB 10,485,760 22,993,475 25,536,410
WIMAXPOSTMONTHLY3GB 3,145,728 26,881,436 204,296
WIMAXPOSTMONTHLY10GB 10,000,000 2,892,547 27,798,041
WIMAXPOSTMONTHLY10GB 10,485,760 8,540,054 17,633,395
WIMAXPOSTMONTHLY3GB 3,145,728 4,639,558 12,137,728
WIMAXPOSTMONTHLY3GB 3,000,000 12,787,712 3,000,000
WIMAXPOSTMONTHLY10GB 10,000,000 14,515,783 5,689,017
|
i want to sum column 2 grouped by column one
so that my out put file has
Quote:
WIMAXPOSTMONTHLY10GB 60,748,736
WIMAXPOSTMONTHLY3GB 53894464
|
|
|
|
05-17-2012, 10:47 AM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
From your example I get different results:
Code:
$ TK_USE_CURRENT_LOCALE=1 awk 'NR > 1{gsub(/,/,"",$2); _[$1]+=$2}END{for (i in _) printf "%s %'\''d\n", i, _[i]}' file
WIMAXPOSTMONTHLY3GB 12,437,184
WIMAXPOSTMONTHLY10GB 51,457,280
The problem for me is to read numbers with thousands separator (hence the gsub statement), whereas the TK_USE_CURRENT_LOCALE=1 and the (escaped) %'d format takes care of the output. Anyway, it should give you an idea, hopefully.
|
|
|
05-18-2012, 06:57 AM
|
#3
|
LQ Newbie
Registered: Feb 2012
Posts: 26
Original Poster
Rep: 
|
how do i also have the sum of the third and fouth column being displayaed too
|
|
|
05-18-2012, 09:20 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
If the suggested solution work on your system and if you understand the code, it should be easy to change it accordingly. Where are you stuck with the awk code?
|
|
|
05-18-2012, 10:37 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
As the commas aren't doing you any favours, just gsub the whole line to remove commas
|
|
|
05-18-2012, 12:10 PM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by grail
As the commas aren't doing you any favours, just gsub the whole line to remove commas
|
Indeed the requirement is ambiguous, since the desired output shown in post #1 is both with and without thousands separators. Anyway, grail are you aware of a method to read numbers in UK notation with GNU awk? Maybe something involving the language settings? 
|
|
|
05-18-2012, 12:51 PM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
Quote:
Originally Posted by colucix
Anyway, grail are you aware of a method to read numbers in UK notation with GNU awk? Maybe something involving the language settings?
|
I did have a quick look but was not able to find any setting that would influence awk as it perceives it strictly as a string and if you force a number it truncates after the first comma.
|
|
|
05-18-2012, 02:12 PM
|
#8
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by grail
I did have a quick look but was not able to find any setting that would influence awk as it perceives it strictly as a string and if you force a number it truncates after the first comma.
|
Exactly what I've experienced. I will deepen my search and if I get some result I will post somewhere for the sake of curiosity. Thanks! 
|
|
|
All times are GMT -5. The time now is 05:32 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|