Enter third column & Perform Operation

I am trying to enter a third column in this file, but the third column should that I call "Math" perform a some math calculations based on the value found in column #2.
Here is the input file:

Here is the desired output:
Output
GERk0203078$ Levir Math
Cotete_1 84412351 (Value of Levir * Constant)/ constant
Cotete_2 39308596 (Value of Levir * Constant)/ constant
Cotete_3 39393962 (Value of Levir * Constant)/ constant

GERk0203040$ Levir
Cotete_1 37235657 (Value of Levir * Constant)/ constant
Cotete_2 84227986 (Value of Levir * Constant)/ constant
Cotete_3 30187827 (Value of Levir * Constant)/ constant

GERk0203050$ Levir
Cotete_1 37217582 (Value of Levir * Constant)/ constant
Cotete_2 30317038 (Value of Levir * Constant)/ constant
Cotete_3 3010017 (Value of Levir * Constant)/ constant

I started this script but not sure how to get the value from column Levir:

#!/usr/bin/ksh
#a is the first constant
#b is the second constant
a=75
b=2500
awk 'NR<=$a {print $1, $2, ("Math" )}' addcolumn > addcolumn.xls
expr ($Levir '*$a') / $b

Any help with that would be greatly appreciated.

I'm assuming you want column 2 * 75 / 2100 ?

awk -v a=$a -v b=$b '{ print $1, $2, ($2*a)/b; }'
1 Like

Can someone tell me what's wrong with this code?

#!/usr/bin/ksh
echo Enter a value for p
read p
a=16384
b=1228800
c=2914
expr $c '*' $c > e
expr $a/$b > x
cat e | expr $x/$e > y
expr $p '*' $y > d
awk -v '{ print $1, $2, ($2*d); }' addcolumn > add.xls

I don't know why you kept -v when you didn't keep all the stuff it's for.

I'm having a hard time telling what you want to do with this code. I tried to show you how to move the expr into awk (where it belongs) but you've just added more of them. (Half of them with incorrect syntax that's probably throwing errors all by itself.) What're all the expr's for? What're you actually trying to do?

---------- Post updated at 09:20 AM ---------- Previous update was at 09:12 AM ----------

Taking your code at face value:

read p
awk -v a=16384 -v b=1228800 -v c=2914 -v p="$p" '
BEGIN {
        e=c*c;
        x=a/b;
        y=x/e;
        d=p*y;
        }
{
        print $1, $2, ($2*d);
}' addcolumn > add.xls

This is what I keep on getting when I try it.

awk: syntax error near line 1
awk: bailing out near line 1

Same issue with previous response

I even added the line below

but to no avail.

---------- Post updated at 11:42 AM ---------- Previous update was at 11:31 AM ----------

All I want to do is to add a 3rd column but want to use the formula to get the values for that column.

---------- Post updated at 11:46 AM ---------- Previous update was at 11:42 AM ----------

Corona, It works with nawk. I appreciate the help.

Did you copy paste what I gave you literally? The adaptation you tried last time needed a lot of work. Show what you actually did.

It'd also help to know what your system is.

I have no idea why it works with nawk but not awk. I tried it on the 'worst' version of awk I have available myself; as far as I know I did nothing that needed nawk.