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?
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
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
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
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.
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.
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
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...
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](file://\\c))"
fi
done
echo
done
exit 0
$ ./calc
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.
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??
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. I don't have time now. Maybe someone else can write the next couple of versions for you.