calculating endless columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculating endless columns
# 8  
Old 10-31-2008
Somehow I cannot understand the algorithm, you said:
Code:
$1/($1+$2)*100,$2/($1+$2),$3/($3+$4)*100

Then you said:

Code:
$4/$3+$4 .....$5/$5+$6, $6/($5,$6)

What's ($5,$6)?
# 9  
Old 10-31-2008
Hi,
The following code could be utilized. Its mostly built upon what has been posted above (by joeyg). A little bit of playing around with that could've helped.

cat file | sed "s/[0-9]*.[0-9]*/&~/g" | tr "~" "\n" | sed 's/$/ ;/g' | awk '{if ($0 != " ;" ) {print $1/($1+$2)","$2/($1+$2)";"} else {print "|";}}' | tr -d "\n" | tr "|" "\n" | sed 's/;/,/g;s/,$//g'
# 10  
Old 11-01-2008
5000 columns will probably exceed your max input line. But if it fits, this ksh solution should work...
Code:
$ cat sample
55 65 48 45 48 68 32 68 44 34 88 65
82 63 52 54 51 68 75 0 0 20 10 77
55 77 60 55 22 60 40 25 75 55 45 90
20 80 33 63 0 64 32 22 75 0 43 56
54 54 12 35 48 87 65 12 77 85 0 15
$
$
$ cat calc
#! /usr/bin/ksh

bc |&
print -p scale=2
exec < sample
while read line ; do
       set -- $line
       while (($#)) ; do
               if (($1 + $2)) ; then
                       print -p "$1 * 100 / ($1 + $2)"
                       read -p result1
                       print -p "$2 * 100 / ($1 + $2)"
                       read -p result2
                       echo $result1 $result2 \\c
                       shift 2
               else
                       echo 00.00 00.00 \\c
               fi
       done
       echo
done
exit 0
$
$
$ ./calc
45.83 54.16 51.61 48.38 41.37 58.62 32.00 68.00 56.41 43.58 57.51 42.48
56.55 43.44 49.05 50.94 42.85 57.14 100.00 0 0 100.00 11.49 88.50
41.66 58.33 52.17 47.82 26.82 73.17 61.53 38.46 57.69 42.30 33.33 66.66
20.00 80.00 34.37 65.62 0 100.00 59.25 40.74 100.00 0 43.43 56.56
50.00 50.00 25.53 74.46 35.55 64.44 84.41 15.58 47.53 52.46 0 100.00
$

# 11  
Old 11-02-2008
Thank you soooo much... really appreciate it..
# 12  
Old 11-09-2008
Hey i have another query . I am trying to add all odd and even columns of the percentages calculated and adding them as columns to the left of the percentages.
so in the above percenages calculated totoal odd would be ($1+$3....= 45.83+51.61...) same thing for even as well

sample output:
t(odd) T(even)
313.28 315.22 45.83 54.16 51.61 48.38 41.37 58.62 32.00 68.00 ......
259.94 380.98 56.55 43.44 49.05 50.94 42.85 57.14 100.00 0

I was trying to embedd the add columns code in the ksh code.. somehow it is not working... and not able to output it as the collumns..


while read line ; do
set -- $line
while (($#)) ; do
if (($1 + $2)) ; then
print -p "$1 * 100 / ($1 + $2)"
read -p result1
print -p "$2 * 100 / ($1 + $2)"
read -p result2
echo $result1 $result2 \\c
shift 2
else
echo 00.00 00.00 \\c
fi
if (($1 +$3)); then
print -p "$1+$3"
read -p addodd
print -p "$2+$4"
read -p addeven
echo addodd addeven
done
echo
done


PS: not too familiar with ksh.. but i think it can be done with ksh

Thank you
# 13  
Old 11-09-2008
can we use an arrary like this ???

array[i][j]
for (i = o,i <2000;,i++)
print i+2
for (j =1; j<2000)
print j+2

Last edited by chronicx; 11-09-2008 at 07:30 PM..
# 14  
Old 11-09-2008
You need to buy a book on shell programming and come up to speed on this stuff. I'm not going to write all of your scripts for you. But here is this one...
Code:
$
$ cat calc
#! /usr/bin/ksh

bc |&
print -p scale=2
exec < sample
while read line ; do
       set -- $line
       output=""
       odd=0.00
       even=0.00
       while (($#)) ; do
               if (($1 + $2)) ; then
                       print -p "$1 * 100 / ($1 + $2)"
                       read -p result1
                       print -p "$2 * 100 / ($1 + $2)"
                       read -p result2
                       output="${output}$(echo $result1 $result2 \\c)"
                       print -p $odd + $result1
                       read -p odd
                       print -p $even + $result2
                       read -p even
                       shift 2
               else
                       output="${output}$(echo 00.00 00.00 \\c)"
               fi
       done
       echo $odd $even "${output}"
done
exit 0
$ ./calc
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
$

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