How to force csh to not use floating point?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to force csh to not use floating point?
# 1  
Old 01-10-2011
How to force csh to not use floating point?

Hi,

I'm using csh, grep, and awk to sum up the third field from a file.
Unfortunately, this results in a very large number and csh
creates a floating point value such as 3.11745e+06.
I was hoping to get 3117450.

I get this error when I run this portion of the csh script and I suspect the variable "dpram" gets the floating point value and "bc" or "sed" cannot handle this.

(standard_in) 1: parse error

CSH program test.csh: (shortened version)
#!/bin/csh
foreach fn ($1/*_gates_power.rpt)
set dpram = `grep "^DPRAM_" $fn | grep -v " 0.0 " | awk '{x += $3 }; END {print x}' | bc | sed 's/[.].//' `
echo $dpram
end

Input file : xyz_gates_power.rpt

DPRAM_2048X8 12 4888.00 1396415.76
DPRAM_2048X8_worst_lib 1 0.0 134956.76

The purpose of " bc | sed 's/[.].//' " is to convert decimal value to integer.
37.0 to 37

Unix cmd-line : unix> test.csh reports

Thanks,

David
# 2  
Old 01-10-2011
You can do the whole grep/awk/bc/sed thing using just awk.

For example using your example file:
Code:
$ awk '$3 != "0.0" { printf "%d\n", $3 }' xyz_gates_power.rpt
4888
$

# 3  
Old 01-12-2011
Since I'm using a foreach loop, I want to sum up the total from all the files.

I'm not too familiar with awk, but can I do this,
haved nested operations to keep a running total?

foreach fn ($1/*_gates_power.rpt)
set dpram = awk '$3 != "0.0" { printf "%d\n", {x+=$3} }' $fn
end
# 4  
Old 01-12-2011
Awk can sum/aggregate columns across multiple files.
Code:
$ cat file1 file2
DPRAM_2048X8 12 4888.00 1396415.76
DPRAM_2048X8_worst_lib 1 0.0 134956.76
DPRAM_2048X8 12 5893.33 1396415.76

DPRAM_2048X8 12 1888.00 1396415.76
DPRAM_2048X8_worst_lib 1 0.0 134956.76
DPRAM_2048X8 12 8813.82 1396415.76

$ cat demo
#!/bin/awk -f

$3 != "0.0" { sum += $3 }
END { printf "%d\n", sum }

$ ./demo file1 file2
21483

# 5  
Old 01-14-2011
Thanks a lot. This works.
Looks like awk can handle large numbers easily without converting
to floating point like csh does.

---------- Post updated at 02:57 PM ---------- Previous update was at 12:04 PM ----------

Now I have a different issue.
I want to keep a running count (static) when I loop thru the foreach
and then echo back the final sum total.
However, this doesnt' work. Every time the loop occurs $SUM is initialized back to zero.
Am I missing something?

#!/bin/csh
# USAGE
# reports is the rc synthesis report files
# TEST.csh reports
setenv UNZIP_HOME `pwd`
echo $UNZIP_HOME
##pushd $1
echo $1
##ls *.gz
#rm $1/*_gate_count.sum
#rm $1/*_DPRAM.sum
#rm $1/*_SPRAM.sum
#rm $1/*_DPRF.sum
#rm $1/*_SPRF.sum
set SUMMARY = 0
foreach fn ($1/*_gates_power.rpt)
set rom = 0
set dpram = 0
set dprf = 0
set spram = 0
set sprf = 0
set TOTAL = 0
#echo '3.7 * 10' | bc | sed 's/[.].//'
#37
#echo $fn
ls $fn
echo "TOTAL ROM AREA IS "
set rom = `grep "^ROM_" $fn | awk '$3 != "0.0" {sum += $3 } END {printf "%d\n", sum}'`
echo $rom
echo "TOTAL DPRAM AREA IS "
set dpram = `grep "^DPRAM_" $fn | awk '$3 != "0.0" {sum += $3 } END {printf "%d\n", sum}'`
echo $dpram
echo "TOTAL DPRF AREA IS "
set dprf = `grep "^DPRF_" $fn | awk '$3 != "0.0" {sum += $3 } END {printf "%d\n", sum}'`
echo $dprf
echo "TOTAL SPRAM AREA IS "
set spram = `grep "^SPRAM_" $fn | awk '$3 != "0.0" {sum += $3 } END {printf "%d\n", sum}'`
echo $spram
echo "TOTAL SPRF AREA IS "
set sprf = `grep "^SPRF_" $fn | awk '$3 != "0.0" {sum += $3 } END {printf "%d\n", sum}'`
echo $sprf
@ TOTAL = ($rom + $dpram + $dprf + $sprf + $spram)
echo "TOTAL MEMORY AREA FOR $fn IS " $TOTAL
@ SUMMARY = $SUMMARY + $TOTAL;
echo "RUNNING TOTAL IS " $TOTAL
end


reports/lahaina_65g_aud_gates_power.rpt
TOTAL ROM AREA IS
0
TOTAL DPRAM AREA IS
49602
TOTAL DPRF AREA IS
178003
TOTAL SPRAM AREA IS
0
TOTAL SPRF AREA IS
0
TOTAL MEMORY AREA FOR reports/lahaina_65g_aud_gates_power.rpt IS 227605
RUNNING TOTAL IS 227605


reports/lahaina_65g_top_gates_power.rpt
TOTAL ROM AREA IS
27947
TOTAL DPRAM AREA IS
3142250
TOTAL DPRF AREA IS
3134294
TOTAL SPRAM AREA IS
5472191
TOTAL SPRF AREA IS
1684165
TOTAL MEMORY AREA FOR reports/lahaina_65g_top_gates_power.rpt IS 13460847
RUNNING TOTAL IS 13460847
# 6  
Old 01-15-2011
Code:
#!/bin/awk -f

function totals(fn)
{
    total = romsum + dpramsum + dprfsum + spramsum + sprfsum
    summary += total

    printf "TOTAL ROM AREA IS: %d\n", romsum
    printf "TOTAL DPRAM AREA IS: %d\n", dpramsum
    printf "TOTAL DPRF AREA IS: %d\n", dprfsum
    printf "TOTAL SPRAM AREA IS: %d\n", spramsum
    printf "TOTAL SPRF AREA IS: %d\n", sprfsum
    printf "TOTAL MEMORY AREA FOR %s: %d\n", fn, total
    printf "RUNNING TOTAL IS %d\n\n", summary

    romsum = dprmasum = dprfsum = spramsum = sprfsum = 0
}

BEGIN { fname = "" }

{
   if (fname == "") fname = FILENAME
   else {
       if (fname != FILENAME) { totals(fname); fname = FILENAME; }
   }
}

/ROM_/   && $3 != "0.0" { romsum += $3 }
/DPRAM_/ && $3 != "0.0" { dpramsum += $3 }
/DPRF_/  && $3 != "0.0" { dprfsum += $3 }
/SPRAM_/ && $3 != "0.0" { spramsum += $3 }
/SPRF_/  && $3 != "0.0" { sprfsum += $3 }

END { totals(fname) }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

2. Shell Programming and Scripting

Arithmetic in floating point

is it not possible to simply di aritmetic without using bc or awk i have tried folllowing operatrions but they support only integer types plz suggest me code for floating using values stored in the variables.the ans i get is integer and if i input floating values i get error numeric constant... (6 Replies)
Discussion started by: sumit the cool
6 Replies

3. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

4. Shell Programming and Scripting

how to compare 2 floating point no.

Hi, Could any one tell me how to compare to floating point no. using test command. As -eq option works on only intergers. i=5.4 if then echo "equal" else echo "not equal" fi here output will be equal even though no. are unequal. Thanks, ravi (1 Reply)
Discussion started by: useless79
1 Replies

5. Programming

Floating point Emulator

what is floating point emulator(FPE)? where and why it is used? (1 Reply)
Discussion started by: pgmfourms
1 Replies

6. Linux

Floating Point Exception

Hi, I am compiling "HelloWorld" C progam on 32-bit CentOS and i want to execute it on 64-bit CentOS architecture. For that i copied the a.out file from 32-bit to 64-bit machine, but while executing a.out file on 64bit machine I am getting "Floating point exception error". But we can run... (3 Replies)
Discussion started by: Mandar123
3 Replies

7. Linux

Floating point exception !!!

Hi, I have linux fedora 4 ver., 2.6 kernal. And qmail & mysql & samba servers are already configured on this server. When I try to install any package like squidguard ,dansguardian,webmin,rsnapshots with command rpm -ivh . It is giving error as “Floating point exception" Snap View is... (3 Replies)
Discussion started by: ssk01
3 Replies

8. Shell Programming and Scripting

Rounding off the value of Floating point value

Hello, i have some variables say: x=1.4 y=3.7 I wish to round off these values to : x = 2 (after rounding off) y = 4 (after rounding off) I am stuck. Please help. (7 Replies)
Discussion started by: damansingh
7 Replies

9. Programming

floating point problem

Hi all! Hi all! I am working with a problem to find the smallest floating point number that can be represented. I am going in a loop ,stating with an initial value of 1.0 and then diving it by 10 each time thru the loop. So the first time I am getting o.1 which I wanted.But from the next... (4 Replies)
Discussion started by: vijlak
4 Replies

10. Shell Programming and Scripting

Floating Point Division

Does anyone have a simple way of doing floating point ("fp") division? For example, if I divide 3 by 5, I can get 0.6. The built-in calc (`bc`) will perform fp multiplication, but not division, at least not straight-up (i.e., starting bc and just typing in 3/5). I am trying to do this using... (1 Reply)
Discussion started by: gsatch
1 Replies
Login or Register to Ask a Question