calculating endless columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculating endless columns
# 15  
Old 11-10-2008
Thank you soo much. I do intend to buy a book on it..

since the total of odd and even column have become $1 and $2 respectively, i tried to do few substitions to divide the $3/$1 and $4/$2 ...$5/$1, $6/$2...but i seem to run into shift problems.


$
$ cat calc
#! /usr/bin/ksh
bc |&
print -p scale=2
exec < sample
while read line ; do
set -- $line
output=""
while (($#)) ; do
if (($3 / $1)) ; then
print -p "$3/$1)"
read -p result1
print -p "$4 / $2)"
read -p result2
output="${output}$(echo $result1 $result2//c"
shift 2
else
output="${output}$(echo 00.00 00.00
\\c)"
fi
done
echo
done
exit 0
$ ./calc

PS: help one last time. .pleasee...:-)
# 16  
Old 11-10-2008
In the code you set odd and even as 0.00..
does it mean that the variables are preset to 2 decimal places???
what is it doing ???
# 17  
Old 11-10-2008
I really have no idea what you're talking about. The script does what you asked... just cut and paste it.

The input data is considered two columns at a time and they are $1 and $2. The "shift 2" makes the current $1 and $2 vanish. Then what used to be $3 and $4 become the new $1 and $2. The odd and even column is $odd and $even. They must get reset to zero for each line of input. And normally echo would output lines including a newline character at the end. You don't want two numbers per line so we need to surpress that newline character and \\c does that. We need to store the calculated %'s in output, because we won't have a finished even and odd until we finish with the line.
# 18  
Old 11-10-2008
okk.. understood the description... i did copy and paste the code... but i i had to divide the even columns by the sum of even columns and odd by sum of odd columns.. $3/$1, $4/$2,$5/$1,$6/$2......would the code in red be right??

if (($3 + $4)) ; then
print -p "$3 * 100 / ($1)"
read -p result3
print -p "$4 * 100 / ($2)"
read -p result4#
echo $result3 $result4 \\c
shift 2
else
echo 00.00 00.00 \\c

Last edited by chronicx; 11-10-2008 at 04:11 PM..
# 19  
Old 11-10-2008
Well I finally understand that "endless" in the title.

I don't see anything in red. But you're not very close. It's wrong to keep trying to use $3 and $4. The totals are calculated during the loop. Until the loop finishes, there no totals available. You will need a second loop to reprocess the data after the totals have been found.

Had you given the real requirements on the weekend I could have written the script you needed instead of one you can't use. Smilie I don't have time now. Maybe someone else can write the next couple of versions for you.
# 20  
Old 11-12-2008
awk '{
for(i=1;i<=NF;i++)
{ if(i%2)
printf("%.2f ",$i*100/($i+$(i+1)))
else
printf("%.2f ",$i*100/($i+$(i-1)))
}
print ""
}' filename
# 21  
Old 11-12-2008
Thank you..i really appreciate your help.. however it is givin out wrong output..
Code:
47.46 52.54 45.83 54.17 51.62 48.38 41.37 58.63 32.00 68.00 56.42 43.58 57.52 42.48 
43.33 56.67 56.56 43.44 49.05 50.95 42.85 57.15 100.00 0.00 0.00 100.00 11.49 88.51 
45.54 54.46 41.66 58.34 52.18 47.82 26.82 73.18 61.54 38.46 57.70 42.30 33.33 66.67 
42.84 57.16 20.00 80.00 34.37 65.63 0.00 100.00 59.26 40.74 100.00 0.00 43.43 56.57 
40.51 59.49 50.00 50.00 25.53 74.47 35.55 64.45 84.42 15.58 47.53 52.47 0.00 100.00

actual input
Code:
284.73 315.22 45.83 54.16 51.61 48.38 41.37 58.62 32.00 68.00 56.41 43.58 57.51 42.48
259.94 340.02 56.55 43.44 49.05 50.94 42.85 57.14 100.00 0 0 100.00 11.49 88.50
273.20 326.74 41.66 58.33 52.17 47.82 26.82 73.17 61.53 38.46 57.69 42.30 33.33 66.66
257.05 342.92 20.00 80.00 34.37 65.62 0 100.00 59.25 40.74 100.00 0 43.43 56.56
243.02 356.94 50.00 50.00 25.53 74.46 35.55 64.44 84.41 15.58 47.53 52.46 0 100.00


sample output of the values to turn out -
16.09 17.18 18.12 15.34 .....

dividng $3/$1 , $4/$2, $5/$1, $6/$2......................

Last edited by chronicx; 11-12-2008 at 04:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculating correlations across columns in awk

Hello, I came across a previous thread "awk-calculating-simple-correlation-rows" which calculated correlations across rows in awk. Code: awk '{ a = 0; for (i = 2; i <= NF; ++i) a += $i; a /= NF-1 b = 0; for (i = 2; i <= NF; ++i) b += ($i - a) ^ 2; b = sqrt(b) if... (7 Replies)
Discussion started by: Ross
7 Replies

2. UNIX for Beginners Questions & Answers

Help with accidental endless loop

I was practicing writing simple loops as I am a new bash user and I created this script, which turned out to be an endless loop where the echo output does not stop and I do not see where my mistake is. #!/bin/bash echo 'enter a number from 1 to 100' read number while do ... (2 Replies)
Discussion started by: goldenlinx
2 Replies

3. Shell Programming and Scripting

Script runs in endless loop

Hi, AM very new to shell scripting and try to run a simple do while loop statement, but it ends up running endlessly. please can anyone assist, dunno what am doing wrong, any useful suggestions will be welcomed. #!/bin/ksh ### To check a running process instance #################... (5 Replies)
Discussion started by: bayoo
5 Replies

4. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

5. Shell Programming and Scripting

[Solved] Endless while loop when compare files

Hi All, I've written a script to read 2 files and compare the contents using while loop but somehow when $line is not found in test2, the script will continue looping. Below is my code, pls advise what could went wrong TIA Nick for line in test1.txt | while read line do grep -i... (4 Replies)
Discussion started by: Nick1971
4 Replies

6. Shell Programming and Scripting

KSH - Issue with endless loop.

First time post. I did a search so I didn’t see this specific issue. It seems to be a head scratcher for me. I have an hourly job that on rare occasions, gets into an endless loop. I’ve tried different scenarios but the current version does basically the following. Find all the *.arc files and... (18 Replies)
Discussion started by: Sylvan303
18 Replies

7. Shell Programming and Scripting

Preventing an endless loop with recursive grep

When finding a string in files within a directory, one can use this: grep -r "searchstring" dir/subdir/ > listofoccurrences.txt For brevity sake one can enter the intended directory and use this: grep -r "searchstring" . > listofoccurrences.txt which as I found out leads to an endless loop,... (2 Replies)
Discussion started by: figaro
2 Replies

8. Shell Programming and Scripting

Endless Loop

Hi, I'm pretty new to UNIX shell scripting and need some help. We have an Informatica interface that dumps any files that have errors into a directory. I need to check that directory periodically for any of up to 9 files that might be in it and run a specific process for each file found. The... (3 Replies)
Discussion started by: JeffR
3 Replies

9. HP-UX

printer spewing out endless copies

Hi I've set up a remote IP-addressed printer using lpadmin on a hp-ux server. It is - as far as I can tell - identical to another printer on the system of the same model type. The default printer works fine with the lp command; the new printer sends endless copies and the job has to be cancelled... (2 Replies)
Discussion started by: ewans
2 Replies

10. Shell Programming and Scripting

Endless loop - Fork function failed?

I need a quick script that will serve as a sort of "real time monitor" for watching some log files. I am using Bourne shell in HP-UX 10.20. I have basically created a script that never ends, unless of course I manually terminate it. Here's the script (it's called qhistory): clear echo "REAL... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question