Getting the sum


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Getting the sum
# 1  
Old 12-10-2011
Getting the sum

I am trying to get the sum of the first column of a file. When I use the same method for other files it works just fine... for some reason for the file below it gives me an error that I don't understand... I tried looking at different lines of the file and tried different things, but I still don't know what the problem is... any idea?

This is my input:

Code:
awk '{sum+=$1} END {print "The sum for age 4Myr with a reddening of 00 = ",sum}' ResultOfChiNGC1856A.dat

And this is the error I get (and I don't understand!)

Code:
awk(10378)  malloc: *** error for object 0x10086e400: incorrect checksum for freed  object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap

The file ResultOfChiNGC1856A.dat is below.

Thank you so much in advance!

(I can't attch/ post the file, it is too big!)
# 2  
Old 12-11-2011
Your last line answered your own question - the file may have too many rows for your version of awk. Or too much data.

Code:
ls -l ResultOfChiNGC1856A.dat
wc -l ResultOfChiNGC1856A.dat

What do these commands show? And important: does wc -l show the number of rows you expected in the file? I am asking in case of file corruption. Barring corruption the file may be REALLY large, hence the ls -l question.

One for you - what is χ NGC 1856a - I can't seem to look up that NGC number 1856?
# 3  
Old 12-11-2011
Quote:
Originally Posted by jim mcnamara
Your last line answered your own question - the file may have too many rows for your version of awk. Or too much data.

Code:
ls -l ResultOfChiNGC1856A.dat
wc -l ResultOfChiNGC1856A.dat

What do these commands show? And important: does wc -l show the number of rows you expected in the file? I am asking in case of file corruption. Barring corruption the file may be REALLY large, hence the ls -l question.

One for you - what is χ NGC 1856a - I can't seem to look up that NGC number 1856?

What confuses me is the 8 files were created in the same exact way (shown below) the sum script works for 6 of them and doesn't work for 2!!
The number of line (wc -l) is 165 as expected. Here is how these files were created (that's why I am not getting what is different about getting the sum of 2 of these ResultChiNGC*): SmilieSmilieSmilie

Code:
awk '{print "awk \47{gsub(\/FileHere\/,\"GALEVw"$2".dat\");print}\47 Step2.dat > ApplyGALEVw"$2".exe"}' ClustersWNames.dat > SpecifyChiSqrt.exe
chmod +x *exe
./SpecifyChiSqrt.exe
awk '{print "./ApplyGALEVw"$2".exe > ResultOfChi"$2".dat"}' ClustersWNames.dat > ApplyChiSqrt.exe
chmod +x *exe
./ApplyChiSqrt.exe

(NGC1985 is a star cluster in the LMC)
# 4  
Old 12-11-2011
$1 should be a number. Correct?
Code:
cut -d ' ' -c 1   ResultOfChiNGC1856A.dat

will show you what column one is. With 165 lines you should be able to see anomalous data.

Sometimes data will have non-printing characters that cause big problems. If that is the case (meaning you saw nothing from try #1 above):

Code:
cut -d ' ' -c 1   ResultOfChiNGC1856A.dat | cat -v

You will see octal numbers like "^037" or "^?" with non-ascii (>127) if there there are problems. The carat precedes a non-printing character.
# 5  
Old 12-11-2011
Also, it looks like you are writing to a compiled intermediate file that you then execute? What exactly is the .exe file?

That is sufficiently off-the-wall to merit looking into as well. JUst because it works sometimes does not allow us to assume it always works correctly.

And what UNIX are you on?
# 6  
Old 12-11-2011
Quote:
Originally Posted by jim mcnamara
Also, it looks like you are writing to a compiled intermediate file that you then execute? What exactly is the .exe file?

That is sufficiently off-the-wall to merit looking into as well. JUst because it works sometimes does not allow us to assume it always works correctly.

And what UNIX are you on?
This is what I get (it looks like something is missing?):


Code:
cut -d ' ' -c 1   ResultOfChiNGC1856A.dat
usage: cut -b list [-n] [file ...]
       cut -c list [file ...]
       cut -f list [-s] [-d delim] [file ...]

---------- Post updated at 12:26 PM ---------- Previous update was at 12:24 PM ----------

This is the *.exe file:

Code:
head -3 SpecifyChiSqrt.exe 
awk '{gsub(/FileHere/,"GALEVwNGC1711all.dat");print}' Step2.dat > ApplyGALEVwNGC1711all.exe
awk '{gsub(/FileHere/,"GALEVwNGC1856A.dat");print}' Step2.dat > ApplyGALEVwNGC1856A.exe
awk '{gsub(/FileHere/,"GALEVwNGC1903A.dat");print}' Step2.dat > ApplyGALEVwNGC1903A.exe

# 7  
Old 12-11-2011
cut -d ' ' -c 1 ResultOfChiNGC1856A.dat --> you're using a delimiter which will cut input into fields, so you cannot use -c switch which will cut out characters.

To delimit input (with ' ' as FS) and print field 1, try cut-d' ' -f1 or if you want the first character alone from input, try cut -c1
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of Columns

HI Guys, I gave Input file F.Txt ID H1 H2 H3 H4 H5 A 5 6 7 8 9 B 4 65 4 4 7 C 4 4 4 4 4 D 4 4 4 4 4 Output :- ID H1 H2 H3 H4 H5 Total 17 79 19 20 24 Sum of Each Columns (8 Replies)
Discussion started by: pareshkp
8 Replies

2. Shell Programming and Scripting

How to get the sum?

hi Gurus, I have an extract as seen below: INPUT 2015-08-24 15:00:00.0 |TCSERVER01 |ServiceEventHandler |2283 2015-08-24 15:01:00.0 |TCSERVER01 |ServiceEventHandler |576 2015-08-24 15:02:00.0 |TCSERVER01 |ServiceEventHandler |833 2015-08-24 15:03:00.0 |TCSERVER01 |ServiceEventHandler... (6 Replies)
Discussion started by: reignangel2003
6 Replies

3. UNIX for Dummies Questions & Answers

Sum with condition

Dear masters, I have file input 011171646073|12129|12129|A027|20141001|20141015|2014|10|01|2013|10|15 011171646076|12129|12129|A027|20141001|20141012|2014|10|01|2014|09|12 011171646078|12129|12129|A027|20141001|20141015|2014|10|01|2014|10|15... (6 Replies)
Discussion started by: radius
6 Replies

4. Shell Programming and Scripting

Sum of all columns

Hi Friends, I have a file with fields separated with comma. How to print sum of each field of the file? Eg: input file 1,3,6,7 2,1,2,1 0,1,1,0 I want to sum each field separately. Output file 3,5,9,8 Thanks, Suresh (2 Replies)
Discussion started by: suresh3566
2 Replies

5. Shell Programming and Scripting

Format the value of sum

I have a list of values ( in Kb) I have the following code to sum up the values and convert the total to GB cat list 701368101370 101370101370 801554101370 701636101370 101757101370 101876101370 901951101370 And this is the output of my script awk '{ s += $1 } END {... (3 Replies)
Discussion started by: Sara_84
3 Replies

6. Shell Programming and Scripting

How to sum these value.

Hi, Unix Gurus, I need sum values from a file. file format like: 0004004 0000817 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0004406 the result should be 459227 (817+45000 ... + 4406) anybody can help me out (7 Replies)
Discussion started by: ken002
7 Replies

7. Solaris

How to Sum

Hi I need to incorporate a 'sum' as follows into a script and not sure how. I have a variable per line and I need them to be summed, e.g below 1 23 1,456 1 1 34 46 How do I calculate the sum of all these numbers to ouptut the answer ( 1,562) Thanks in advance (3 Replies)
Discussion started by: rob171171
3 Replies

8. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

9. Shell Programming and Scripting

sum

Hello everyone I need to write a script that sums numbers passed to it as arguments on the command line and displays the results. I must use a for loop and then rewrite it using a while loop. It would have to output something like 10+20+30=60 this is what I have so far fafountain@hfc:~$ vi sum... (1 Reply)
Discussion started by: Blinky85
1 Replies

10. UNIX for Advanced & Expert Users

sum of more than 1 column

Hi I want to sum of 3 columns in file. Example: I want to sum of 3 ,6,8 th columns in file(SUM(3,6,8)). Using awk can sum of single column awk '{a+=$3} END {printf ("%f\n",a)' file_name Thanks inadvance MR (2 Replies)
Discussion started by: mohan705
2 Replies
Login or Register to Ask a Question