Add edited numeric strings in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add edited numeric strings in awk
# 1  
Old 11-08-2013
Add edited numeric strings in awk

I am using awk to sum up all amounts and at end print total.

input:
10,250.00
20,103.15



expected output:
30353.15

code:
{subtot=+$1} END{print subtot}

The problem I encounter is it stops at commas and returns 30 as answer

And if I use this code:
{subtot=($1+0);subtot=+$1} END{print subtot}

it returns 40

any help, please.
# 2  
Old 11-08-2013
Quote:
Originally Posted by paresh n doshi
I am using awk to sum up all amounts and at end print total.

input:
10,250.00
20,103.15



expected output:
30353.15

code:
{subtot=+$1} END{print subtot}

The problem I encounter is it stops at commas and returns 30 as answer

And if I use this code:
{subtot=($1+0);subtot=+$1} END{print subtot}

it returns 40

any help, please.

Try

Code:
$ awk '{sum+=$1}END{print sum}' file

if comma is there then use this

Code:
$ printf "%s\n%s\n" 10,250.00 20,103.15 | awk '{gsub(/,/,x,$1);sum+=$1}END{print sum}'
30353.2

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 11-08-2013
remove the commas:
Code:
awk '{gsub(/,/,"");subtot+=$1}END{print subtot}' file

This User Gave Thanks to Subbeh For This Post:
# 4  
Old 11-08-2013
Thanks. One more query. Is it possible to assign the result of gsub to a variable?
like
Code:
var=gsub(/,/,"",$1)


Last edited by Franklin52; 11-08-2013 at 01:12 PM.. Reason: Please use code tags
# 5  
Old 11-08-2013
you can.
variable will results in to number of times the substitution has been made.
For ex:
Code:
echo "hi,this,is,name" | awk '{ a=gsub(/,/,"");print a}'
3

This User Gave Thanks to greet_sed For This Post:
# 6  
Old 11-08-2013
Quote:
Originally Posted by paresh n doshi
Thanks. One more query. Is it possible to assign the result of gsub to a variable?
like
var=gsub(/,/,"",$1)

Try like this

Code:
$ printf "%s\n%s\n" 10,250.00 20,103.15 | awk '{var=$1;gsub(/,/,x,var);print var}'
10250.00
20103.15

if you assign directly as greet_sed said it gives you a count of substitution
This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 11-08-2013
That var will hold the count of substitutions that gsub has made on the string it worked upon. The string itself will be modified and thus holds the result.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename first N numeric strings in filename

Hello, I have some video files containing numbers and characters . To tell the problem shortly, due to a limitation, I am unable create a playlist regularly changing on a daily basis with the command shuffle....So I decided to rename filenames, just a replacement of first five numbers randomly.... (10 Replies)
Discussion started by: baris35
10 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

filtering a numeric value which has '%' using awk

Hello Gurus, I have a requirement where I have to filter a value from some field which has 99% or greater than '99%'.. For ex: The Date (file -- sample.csv) will be like below Field1,Field2,Field3,Field4 860440512,844284992,16155520,99% 860440512,844284992,16155520,94%... (4 Replies)
Discussion started by: raghu.iv85
4 Replies

5. Shell Programming and Scripting

awk to search similar strings and add their values

Hi, I have a text file with the following content: monday,20 tuesday,10 wednesday,29 monday,10 friday,12 wednesday,14 monday,15 thursday,34 i want the following output: monday,45 tuesday,10 wednesday,43 friday,12 (3 Replies)
Discussion started by: prashu_g
3 Replies

6. UNIX for Dummies Questions & Answers

Listing numeric strings alone

i have a file called 'test.txt' which contains the below data. abc123 123445 123abc 23224q From the above data, i want to display the line which contains only numeric . So i have tried the below commands with sed sed -n '/^$/p' test.txt sed '/^$/!d' test.txt But it... (4 Replies)
Discussion started by: pandeesh
4 Replies

7. Shell Programming and Scripting

sed/awk-adding numeric to a column

I have a txt file as follows Code: Oct 1 file1 4144 Oct 1 file23 5170 Oct 2 file5 3434 Oct 21 file56 2343 I need to add a new column by marking the right log file from current directory. For example populate like this. Please not in the second columt for "1" it has... (2 Replies)
Discussion started by: gubbu
2 Replies

8. Shell Programming and Scripting

AWK: Discrepancy in numeric output

During a file-system cleanup I noticed a strange behavior of awk (HP-UX 11iv3 / IA64). When summing up the size of files in one directory it gives different numbers when using print as opposed to printf: find . -type f -name '*.dmp.Z' -mtime +35 -exec ls -l {} \+ | \ awk 'BEGIN{ OFMT="%f" } {... (1 Reply)
Discussion started by: pludi
1 Replies

9. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

10. Shell Programming and Scripting

How To Add Numeric Data of A file

Dear All, I want to add Numeric data of my file called temp_f41. But I am not getting how to add them, My File data are in following format of: 10 39 53 05 37 54 Plz send me code for this Thanks Nishant :mad: (3 Replies)
Discussion started by: krishna_sicsr
3 Replies
Login or Register to Ask a Question