calculating endless columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculating endless columns
# 1  
Old 10-31-2008
calculating endless columns

I have about 5000 columns of data that i need to convert all of it into pecentages. for shorter colums i have been using this code:
{print $1/($1+$2)*100,$2/($1+$2),$3/($3+$4)*100 .....}
but this is a teadious process... is there anyway to do it without having to write all of them out?

sample data:
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

Output data:
45.83 54.16 51.61 48.38 .................... (all the columns are in pairs..$1 and $2 are a pair.. so while calculating column 3 and 4.. it would be $3/$3+4 and $4/$3+$4 .....$5/$5+$6, $6/($5,$6)

PS: there will be lines that will have to be divided by zero. in those cases i am using this code.. is it right? need to embedd it into the overall code as well..
if ($0 != 0) lines++; print 0

Thank you.Smilie
# 2  
Old 10-31-2008
Question

Does it matter to keep the stuff on the originating line?
I am imagining a solution would would create a long list...
# 3  
Old 10-31-2008
ya i can go on putting the code till those endless colums.. but that would take long and prone to errors..well... doesnt matter where it is.. as long as it works
# 4  
Old 10-31-2008
Hammer & Screwdriver OK, here is my start at it...

Command instructions
I break up pairs of numbers, and put a ~ marker
get rid of new-line (carriage returns)
replace the ~ after each pair with a new-line; putting pairs on own rows
then do the awk command
Code:
> cat file30 | sed "s/[0-9]*.[0-9]*/&~/g" | tr "\n" " " | tr "~" "\n" | awk '$1*$2>0,z=$1+$2;x=$1/z;y=$2/z {print x,y}'

First couple enty outputs
it is repeating my inputs twice, & not sure why yet (still thinking about that)
then appears to be doing your % as requested
Code:
55 65
55 65
0.458333 0.541667
 48 45
 48 45
0.516129 0.483871

Perhaps this can get you started?
The awk needs to be 'finished'.

Also, the awk might be able to be augmented to use ~ as the RS (record separator). I just had issues trying to get that to work quickly.
# 5  
Old 10-31-2008
hmm.. wat if the output should come out as per the input.. like corresponding to the output.?

Last edited by chronicx; 10-31-2008 at 04:23 PM..
# 6  
Old 10-31-2008
anyone??
# 7  
Old 10-31-2008
Data Beware of posting to elicit a quicker response

FORUM RULES...
(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post or send a private message where your goal is to get an answer more quickly.

There are many who try to answer here, but it is always on an "as possible" basis. Sometimes it might take a few days to get to a good answer -- all depends on when people have a chance to read through questions.
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