Totals in a file - incorrectly displaying


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Totals in a file - incorrectly displaying
# 1  
Old 12-13-2010
Totals in a file - incorrectly displaying

Afternoon,

I have a script which creates/modifies data into a formatted csv.

The trailer record should display 2 columns, the first is a static entry of "T" to identify it as a trailer record. The 2nd is a total of amounts in a column throughout the entire file.

My total isn't displaying correctly. I do not use decimal points and the following code is not displaying the final digit. It displays every other digit correctly (just missing the 2nd digit after the decimal point)

At the minute I have
Code:
 $(awk '{s+=$3}END {print "T, "s}

On a seperate note, considering it is the 3rd column I am totalling, the header record has 3 columns which I do not want included - it seems to work just now ever though the header has a fixed number in the 3rd column (does awk count the $'s from 0 or from 1?)

Thanks
# 2  
Old 12-13-2010
it counts from $1 ... $NF

Use printf("T,%15.4\n", s)

15 = size of field, .4 = number characters after decimal, the precision.

When you ask questions like this examples are much better: like expected output. The answer I just gave you could be wrong....
# 3  
Old 12-13-2010
Ah ok...actual output =
Code:
  
A, blah, 091929
C, 123491,4999,13122010,C, 8281946 MR BRIAN BOBS  ,ND
T, 499

Expect output =
Code:
  
A, blah, 091929
C, 123491,4999,13122010,C, 8281946 MR BRIAN BOBS  ,ND
T, 4999

As you can see the trailer record should hold both decimal places, but it doesn't. Considering the file is a csv, the field length is variable - the only thing I can confirm is the last 2 digits in the column will be the 2 digits on the right of the decimal place.

I appreciate your help
# 4  
Old 12-13-2010
given your sample input:
Code:
$ awk -F, '{s+=$3}1;END {print "T ",s}' OFS=, myFile
A, blah, 091929
C, 123491,4999,13122010,C, 8281946 MR BRIAN BOBS  ,ND
T, 96928

What's wrong with this output?
# 5  
Old 12-13-2010
AH, I have remember something.

I add the header record myself near the end of the script, by the time I have done this I have already created the trailer record - meaning the total is complete and doesn't use the header.

Anymore thoughts as to why my code is not correctly using 2 decimal places?
# 6  
Old 12-13-2010
Quote:
Originally Posted by mcclunyboy
AH, I have remember something.

I add the header record myself near the end of the script, by the time I have done this I have already created the trailer record - meaning the total is complete and doesn't use the header.

Anymore thoughts as to why my code is not correctly using 2 decimal places?
Hmmm...... I don't quite understand your question and/or reference to the '2 decimal places'.
Can you maybe rephrase it as I don't see any 'decimal places' - you're dealing with integers.
Does the alternate solution solve your issue?
# 7  
Old 12-13-2010
sorry, the 3rd column in my sample is basically an amount field. I have removed the decimal places for other reasons. Anyway, as it is an amount field the the final 2 digits are the 2 on the right of the decimal place.

Even so, my problem is the code I am using is not correctly displaying the total, it is missing the right hand digit.
$(awk '{s+=$3}END
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Large File masking incorrectly happening Ç delimeter issue

The OS version is Red Hat Enterprise Linux Server release 6.10 I have a script to mask some columns with **** in a data file which is delimeted with Ç , I am using awk for the masking , when I try to mask a small file the awk works fine and masks the required column , but when the file is... (6 Replies)
Discussion started by: LinuxUser8092
6 Replies

2. UNIX for Beginners Questions & Answers

Ls incorrectly says file not found in sftp.

Hi all When I sftp to an Oracle cloud server, to a folder where there are more than 10k files, and list a small subset of files, it works OK. When I try list more than 10k files, it says file not found. Example output below. (FYI ls/mget with 6k files works OK on this server) Has anyone... (3 Replies)
Discussion started by: pdinsdale
3 Replies

3. Shell Programming and Scripting

Grand totals in awk

I have a one-liner script like this that gives a total of everything in various directories: for i in *; do (cd $i && cd statelist && echo $i && ls -la |awk 'NR>3 {SUM += $5}\ END { print "Total number of elements " SUM }');done It works just great but at the end I want to print a grand... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. Shell Programming and Scripting

Trying to combine upload and download totals from txt file by ip address

I have two files, uploads.txt and downloads.txt. I would like to combine the columns of these files based on the ip address. How can I best do this? Uploads.txt 192.168.0.147 1565369 192.168.0.13 1664855 192.168.0.6 1332868 Downloads.txt 192.168.0.147 9838820 192.168.0.18 12051718... (7 Replies)
Discussion started by: zanyspydude
7 Replies

5. Shell Programming and Scripting

Report Totals

Hello, I have written a script in a previous server and its being migrated to a new server. I'm trying to debug my script since i've had to make minor changes to it to get it to work. I'm having a hard time getting my totals to populate here is the syntax DUMP_COUNT=`sqlplus -S... (4 Replies)
Discussion started by: senormarquez
4 Replies

6. Shell Programming and Scripting

[Solved] Find Specific records from file and add totals into variables

Hi Eveyone, I am working on one shell script to find the specific records from data file and add the totals into variables and print them. you can find the sample data file below for more clarification. Sample Data File: PXSTYL00__20090803USA CHCART00__20090803IND... (7 Replies)
Discussion started by: veeru
7 Replies

7. Shell Programming and Scripting

summarising totals in awk

awk ' FILENAME == "all" { balance += substr($0,17,13) dt = substr($0,6,8) } END { for ( name in balance ) printf("%013s %3s of %8s\n", balance/100,name,dt) | "sort " } ' all > summation using this code i wanted to take summary totals of... (3 Replies)
Discussion started by: paresh n doshi
3 Replies

8. Shell Programming and Scripting

Calculating totals in AWK

Hello, With the following small script I list the size of documents belonging to a certain user by each time selecting the bytes-field of that file ($7). Now it fills the array with every file it finds so in the end the output of some users contains up to 200.000 numbers. So how can I calculate... (7 Replies)
Discussion started by: Hille
7 Replies

9. Shell Programming and Scripting

Writing to a log file incorrectly

I have this script: #!/bin/ksh ######### Environment Setup ######### PATH=/gers/nurev/menu/pub/sbin:/gers/nurev/menu/pub/bin:/gers/nurev/menu/pub/mac :/gers/nurev/menu/adm/sbin:/gers/nurev/menu/adm/bin:/gers/nurev/menu/adm/mac:/ge... (5 Replies)
Discussion started by: heprox
5 Replies
Login or Register to Ask a Question