Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Sum up formatted numbers with comma separation


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Sum up formatted numbers with comma separation

I need to sum up the values in field nr 5 in a data file that contains some file listing. The 5th field denotes the size of each file and following are some sample values.

Code:
1,775,947,633
4,738
7,300
16,610
15,279
0
0

I tried the following code in a shell script.
Code:
awk '{sum+=$5} END{print sum}' mylogfile.log

But it does not add up the numbers. The result is the sum of values until it encounters the first comma in each of the numbers.

I also tried doing a substitution before summing up (as below), but even that did not give expected result.

Code:
awk '{sum+=gsub(",", "", $5)} END{print sum}' mylogfile.log

Please suggest if you have encountered such a scenario.
# 2  
Hi, did I understand correctly?
Code:
awk -F, 'NR == 5 {for(i = 1; i <= NF; i++) sum+=$i; print sum}' file
294

--- Post updated at 10:30 ---

Code:
awk '{sum = 0; t = split($5, arr, ","); for(i = 1; i <= t; i++) sum+=arr[i]; print sum}' file

fixed

Last edited by nezabudka; 05-27-2019 at 04:36 AM..
# 3  
Not exactly. To explain further, following is the input data.

Code:
1,775,947,633
4,738
7,300
16,610
15,279
0
0

Expected output is (sum of all the numbers):
Code:
1775991560

I am trying to achieve this using awk.

--- Post updated at 02:14 PM ---

solved this using gsub by correcting my arguments to gsub as follows.

Code:
awk '{gsub(/,/,"",$5);sum+=$5} END{print sum}' file

Result:
Code:
1775991560

# 4  
Code:
awk {gsub(",", "", $5); sum+=$5} END {print sum}' file

may be not "$5" field
Code:
awk {gsub(",", ""); sum+=$0} END {print sum}' file


Last edited by nezabudka; 05-27-2019 at 05:50 AM..
# 5  
Supposed you only show $5 it is
Code:
awk '{gsub(",", "", $5); sum+=$5} END{print sum}' mylogfile.log

The gsub() returns the number of performed substitutions, not the result string. The result is stored in the input variable, here $5.

Because modification of an input field like $5 causes a reformatting of $0, it sometimes makes sense to have an extra variable.
A demonstration:
Code:
awk '{x=$5; gsub(",", "", x); sum+=x; print} END{print sum}'

Compare with
Code:
awk '{gsub(",", "", $5); sum+=$5; print} END{print sum}'

These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Split files with formatted numbers
bobbygsk
How to split the file and have suffix with formatted numbers Tried the following code awk '{filename="split."int((NR-1)/2)".txt"; print >> filename}' split.txt Current Result Expected Result... Shell Programming and Scripting
21
Shell Programming and Scripting
Removing comma just from numbers
Johanni
Hi Guys, I want to remove commas from just the numbers in the file. So both sides of the comma should be numbers. Input file Johan 1,234 nb jan 123,3 hi, hello, bye 12,345,54 hejhej Desired output: Johan 1234 nb jan... Shell Programming and Scripting
6
Shell Programming and Scripting
getting the sum of numbers
zerofire123
I basically have a file where I had to do a bunch of greps to get a list of numbers example: a file called numbers.txt 10000 10000 superman 10000 batman 10000 10000 grep '100' * | 10000 10000 10000 10000 10000... Shell Programming and Scripting
2
Shell Programming and Scripting
Extracting formatted text and numbers
DFr0st
Hello, I have a file of text and numbers from which I want to extract certain fields and write it to a new file. I would use awk but unfortunately the input data isn't always formatted into the correct columns. I am using tcsh. For example, given the following data I want to extract: and...... Shell Programming and Scripting
3
Shell Programming and Scripting
Joining three lines with comma separation
BCarlson
I have a file that looks like this: G. KRESSLAR 9618 W. APPALOOSA DRIVE SUN CITY, AZ 85373 SHIRLEY ALLEN 7272 W. VIA MONTOYA DRIVE GLENDALE, AZ 85310 LOUIS VALDEZ 244441 N. 86TH AVENUE PEORIA, AZ 85383 DONNA NEWBON 3231 W. DENTON #D PHOENIX, AZ 85017 SARAH WILSON 6534 W. PALO...... Shell Programming and Scripting
3
Shell Programming and Scripting

Featured Tech Videos