Sum up a decimal column in a tab separated text file and error handling


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sum up a decimal column in a tab separated text file and error handling
# 1  
Old 02-23-2010
Sum up a decimal column in a tab separated text file and error handling

Hi,

I have a small requirement where i need to sum up a column in a text file.

Input file

Code:
66ab		000000			534385			-00000106350.00
66cd		000000			534485			-00013364511.00
66ad		000000			534485			-00000426548.00
672a		000000			534485			000000650339.82
40sC		000000			556020			000000123380.00
61oC		000000			556030			000000006781.00
400C		000000			558119			-00000067384.68
400C		000000			558120			-00000014166.06
011C		000000			558200			000000581707.00
058C		000000			558200			000000094120.80
011C		000000			558300			-00000009760.84

I need to sum up the 4th column with decimals and get the amount in a variable adn i need to get the record count.

How to incorporate error handling, like suppose it skips a row while reading or cannot sum because of the data problem...

I try to use "awk", but its throwing some errors

Any help code will be appreciated.

Thanks in Advance,
SandeepSmilie

Last edited by Franklin52; 02-23-2010 at 02:36 PM.. Reason: Please use code tags!
# 2  
Old 02-23-2010
Code:
/usr/xpg4/bin/awk '{a+=$4}END{printf "%012.2f\n",a}' FILE

,

and using "eval" assing it to a variable:

Code:
eval "echo $( /usr/xpg4/bin/awk '{a+=$4}END{printf "%012.2f\n",a}' FILE )"


Last edited by EAGL€; 02-23-2010 at 02:42 PM..
# 3  
Old 02-23-2010
Quote:
Originally Posted by pssandeep
I try to use "awk", but its throwing some errors
What errors? Show your code.
# 4  
Old 02-23-2010
Hi,

Its a sample code and haven't developed compeltely.
Looking for standard error code normally used as per the UNIX standards

---------- Post updated at 04:30 PM ---------- Previous update was at 04:29 PM ----------

Hi Eagle,

When i add ur code by repalcing the file nam, it running forever..

thanks for your reply. Could you provide me some more inputs on this?

Regards,
Sandeep

---------- Post updated at 05:13 PM ---------- Previous update was at 04:30 PM ----------

Hi,
Code:
Sum= `eval "echo $(awk '{a+=$4}END{printf "%012.2f\n",a}' BO_Report_DAY4.txt)"`
echo $Sum

when i used above mentione dcode, it displaying following error.

Could you let me know what i didi wrong.
Code:
+ awk {a+=}END{printf "%012.2f\n",a} BO_Report_DAY4.txt
 Syntax Error The source line is 1.
 The error context is
                 >>> {a+=} <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
+ eval echo
+ echo
+ Sum=


Thanks
Sandeep

Last edited by Scott; 02-23-2010 at 06:19 PM.. Reason: Code tags please...
# 5  
Old 02-24-2010
ok, then simply you can use SUM as a variable in your loops:

Code:
SUM=`/usr/xpg4/bin/awk '{a+=$4}END{printf "%012.2f\n",a}' FILE`

# 6  
Old 02-24-2010
Hi,

thanks its working. Its not working whn i am using complete path for "awk"

One other question how to do error handling on this command,

For example, if something fails while adding up or it does not able to add a column because of data problem or it goes to infinite loop.

Do you suggest any best practices and my script should fail if any thing happens

thanks,
Sandeep
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert tab separated text to non-trivial xml. (pwsafe --> KeePassx)

I'd like to take the output of `pwsafe --exportdb > database.txt` and convert it to a KeePassX XML friendly format (feature request in pwsafe). I found flat file converter but the syntax is beyond me with this example. Solutions are welcomed. More details Here is the pwsafe --> KeePassX XML... (2 Replies)
Discussion started by: graysky
2 Replies

2. Shell Programming and Scripting

Read a tab separated file with empty column

Hi all, I'm trying to read a tab separated file and apply some functions on each column. I have an issue with empty column. Exemple: $ #cat with the sed to allow you to see my tab $ cat foo.txt| sed 's/\t/;/g' a;1;x b;;yI wanted to something like that: while read col1 col2 col3 do ... (4 Replies)
Discussion started by: maturix
4 Replies

3. Shell Programming and Scripting

How to replace & with and in tab separated file?

Hi, I have a tab separated. I want to replace all the "&" in 8th column of the file with "and" .I am trying with awk -F, -vOFS=\\t '{$8=($8=="&")?"and":$8}1' test> test1.txt My file is abc def ghk hjk lkm hgb jkluy acvf & bhj hihuhu fgg me mine he her go went has has & had hgf hgy ... (1 Reply)
Discussion started by: jagdishrout
1 Replies

4. Shell Programming and Scripting

Problem with a tab separated file

Hi, I have created a tab separated file from the following input file. ADDRESS1 CITY STATE POSTAL COUNTRY LON LAT 32 PRINZREGENTENSTRASSE ROSENHEIM BAYERN 83022 DEU 1212182 4785699 263 VIA DANTE ALIGHIERI BARI PUGLIA 70122 ITA 1686233 4112154 30 VIA MILANO ... (1 Reply)
Discussion started by: ramky79
1 Replies

5. UNIX for Dummies Questions & Answers

tab-separated file to matrix conversion

hello all, i have an input file like that A A X0 A B X1 A C X2 ... A Z Xx B A X1 B B X3 .... Z A Xx Z B X4 and i want to have an output like that A B C D A X0 X1 X2 Xy B X1 X3 X4 (4 Replies)
Discussion started by: TheTransporter
4 Replies

6. UNIX for Dummies Questions & Answers

Using awk to log transform a column in a tab-delimited text file?

How do I use awk to log transform the fifth column of a tab-delimited text file? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

Add a new column to a tab delimited text file

I want to add a new column to a tab delimited text file. It will be the first column and it will just be 1's. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Sum of decimal numbers in column

Hi!!! I have n decimal numbers in column: 1.23 3.45 5.16 . . . How to do arithmetic sum of theese numbers??? Thanks!!!:D (4 Replies)
Discussion started by: tdev457
4 Replies

9. Shell Programming and Scripting

parse file into tab separated columns

Hello, I am trying to parse a file that resembles the last three groupings into something looking like the first two lines. I've fiddled with sed and awk a bit, but can't get anything to work properly. I need them separated by some delimiter. The file is some 23,000 lines of the stuff.... ... (9 Replies)
Discussion started by: dkozel
9 Replies

10. Shell Programming and Scripting

Delete first column in tab-delimited text-file

I have a large text-file with tab-delimited genetic data that looks like: KSC112 KSC234 0 0 1 1 A G C T I simply wan to delete the first column, but since the file has 600 000 columns, it is not possible with awk (seems to be limited at 32k columns). Does anyone have an idea how to do this? (2 Replies)
Discussion started by: andmal
2 Replies
Login or Register to Ask a Question