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... with help i got this

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}'
problem is it prints the input data before converting to %. also i need to code it in such a way that it does all 5000 columns at once... and outputs to lines corresponding to input..
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.

Hey there,

Hopefully this will help you out. It also avoids the illegal division by zero and just returns 0 in that condition.

I put your values in a file called "awkpct"

host # cat awkpct
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

and ran this command

host # awk 'BEGIN{lasti=1}{for (i=1;i<=NF;i++) {if ( i%2==0 ) {y=$lasti+$i;printf("%.2f %.2f ", $lasti/y*100, $i/y*100)}lasti=i }printf"\n"  }' awkpct

which produces these results:

45.83 54.17 51.61 48.39 41.38 58.62 32.00 68.00 56.41 43.59 57.52 42.48
56.55 43.45 49.06 50.94 42.86 57.14 100.00 0.00 0.00 100.00 11.49 88.51
41.67 58.33 52.17 47.83 26.83 73.17 61.54 38.46 57.69 42.31 33.33 66.67
20.00 80.00 34.38 65.62 0.00 100.00 59.26 40.74 100.00 0.00 43.43 56.57
50.00 50.00 25.53 74.47 35.56 64.44 84.42 15.58 47.53 52.47 0.00 100.00

Best wishes and hope all goes well for you,

Mike

Mike,

Definitely some genius code there.... the one thing you missed is that it needs to skip a column....

I was hoping you could just increment i by 2 - but that doesn't seem to work... I really don't know all that you are doing there, so I was unable to figure it out.

Thanks :)

I was a little confused by the question, actually ;) What I was working on was the assumption that I had to do, per record, was:

field1/field1+field2 - field2/field1+field2 - field3/field3+field4 - field4/field3+field4 etc..

Can you rephrase the original request (I know it's not your question, so apologies) but I think I'm still misreading something that you've noticed.

Speaking of owners, what happened to RockX? If you're out there; any thoughts?

;)

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.